4
4
def reverse_sync_config ():
5
5
print ("Starting reverse config sync..." )
6
6
7
- # Source: the sanitized config in your project
7
+ # Source: sanitized config in your project
8
8
project_dir = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
9
9
source_path = os .path .join (project_dir , "config" , "claude_code_config.json" )
10
10
print (f"Source config path: { source_path } " )
11
11
12
- # Target: the original config location on macOS
12
+ # Target: original config location on macOS
13
13
target_dir = os .path .expanduser ("~/.claude-code-router" )
14
14
target_path = os .path .join (target_dir , "config.json" )
15
15
print (f"Target config path: { target_path } " )
@@ -22,15 +22,39 @@ def reverse_sync_config():
22
22
os .makedirs (target_dir , exist_ok = True )
23
23
24
24
# Read the sanitized config
25
- with open (source_path ) as f :
25
+ with open (source_path , 'r' ) as f :
26
26
config = json .load (f )
27
27
28
- # Write config back to original location
28
+ # Restore API keys from environment variables
29
+ openrouter_api_key = os .getenv ("OPENROUTER_API_KEY" )
30
+ if not openrouter_api_key :
31
+ print ("Warning: OPENROUTER_API_KEY environment variable not set." )
32
+ # Optionally fail fast if key is required
33
+ # raise EnvironmentError("Missing OPENROUTER_API_KEY environment variable")
34
+
35
+ # Replace empty or placeholder API keys
36
+ if "Providers" in config :
37
+ restored = False
38
+ for provider in config ["Providers" ]:
39
+ if (
40
+ "api_key" in provider
41
+ and (not provider ["api_key" ] or provider ["api_key" ] == "REPLACE_WITH_OPENROUTER_API_KEY" )
42
+ ):
43
+ provider ["api_key" ] = openrouter_api_key
44
+ if openrouter_api_key :
45
+ print ("Restored API key for provider." )
46
+ restored = True
47
+ else :
48
+ print ("No valid API key to restore." )
49
+ if not restored :
50
+ print ("No API key needed restoration (already set or no match)." )
51
+
52
+ # Write the restored config back to the original location
29
53
print ("Writing config back to original location..." )
30
- with open (target_path , "w" ) as f :
54
+ with open (target_path , 'w' ) as f :
31
55
json .dump (config , f , indent = 2 )
32
56
33
- print ("Reverse config sync completed successfully" )
57
+ print ("Reverse config sync completed successfully. " )
34
58
35
59
if __name__ == "__main__" :
36
- reverse_sync_config ()
60
+ reverse_sync_config ()
0 commit comments