1616
1717# Team ID validation pattern: CUID (v1)
1818TEAM_ID_PATTERN = re .compile (r"^c[a-z0-9]{24}$" )
19+ PROFILE_OVERRIDE_ENV_VARS = (
20+ "PRIME_API_KEY" ,
21+ "PRIME_TEAM_ID" ,
22+ "PRIME_USER_ID" ,
23+ "PRIME_API_BASE_URL" ,
24+ "PRIME_BASE_URL" ,
25+ "PRIME_FRONTEND_URL" ,
26+ "PRIME_INFERENCE_URL" ,
27+ "PRIME_CONTEXT" ,
28+ )
1929
2030
2131def validate_team_id (team_id : str ) -> bool :
@@ -32,6 +42,34 @@ def validate_team_id(team_id: str) -> bool:
3242 return bool (TEAM_ID_PATTERN .match (team_id ))
3343
3444
45+ def _active_profile_override_env_vars () -> list [str ]:
46+ return [name for name in PROFILE_OVERRIDE_ENV_VARS if (os .getenv (name ) or "" ).strip ()]
47+
48+
49+ def _require_profile_env_unset (command : str ) -> None :
50+ names = _active_profile_override_env_vars ()
51+ if not names :
52+ return
53+ joined = ", " .join (names )
54+ console .print (
55+ f"[red]Error:[/red] { joined } { 'is' if len (names ) == 1 else 'are' } set in your "
56+ f"environment, so [bold]prime config { command } [/bold] cannot make a saved profile "
57+ "active. Unset the environment override and rerun the command."
58+ )
59+ raise typer .Exit (1 )
60+
61+
62+ def _require_no_context_override (command : str ) -> None :
63+ context = Config .context_override ()
64+ if not context :
65+ return
66+ console .print (
67+ f"[red]Error:[/red] [bold]prime config { command } [/bold] cannot update auth while "
68+ f"PRIME_CONTEXT is set to '{ context } '. Run the command without a temporary context."
69+ )
70+ raise typer .Exit (1 )
71+
72+
3573@app .command ()
3674def view () -> None :
3775 """View current configuration"""
@@ -116,6 +154,8 @@ def set_api_key(
116154 ),
117155) -> None :
118156 """Set your API key (prompts securely if not provided)"""
157+ _require_no_context_override ("set-api-key" )
158+
119159 if api_key is None :
120160 # Interactive mode with secure prompt
121161 api_key = typer .prompt (
@@ -126,6 +166,7 @@ def set_api_key(
126166 )
127167
128168 config = Config ()
169+ config .use_production_environment ()
129170 config .set_api_key (api_key )
130171
131172 if api_key :
@@ -140,7 +181,6 @@ def set_api_key(
140181 user_id = data .get ("id" )
141182 if user_id :
142183 config .set_user_id (user_id )
143- config .update_current_environment_file ()
144184 except (APIError , Exception ):
145185 pass
146186
@@ -161,6 +201,7 @@ def set_team_id(
161201 ),
162202) -> None :
163203 """Set your team ID."""
204+ _require_no_context_override ("set-team-id" )
164205 config = Config ()
165206
166207 # Validate team ID format
@@ -186,6 +227,7 @@ def set_team_id(
186227 except (APIError , Exception ):
187228 pass
188229
230+ config .use_production_environment ()
189231 config .set_team (team_id , team_name = team_name , team_role = team_role )
190232 if team_id :
191233 if team_name :
@@ -199,7 +241,9 @@ def set_team_id(
199241@app .command ()
200242def remove_team_id () -> None :
201243 """Remove team ID to use personal account"""
244+ _require_no_context_override ("remove-team-id" )
202245 config = Config ()
246+ config .use_production_environment ()
203247 config .set_team (None )
204248 console .print ("[green]Team ID removed. Using personal account.[/green]" )
205249
@@ -278,6 +322,7 @@ def _set_environment(
278322 env : str ,
279323) -> None :
280324 """Set URLs for a specific environment"""
325+ _require_profile_env_unset (f"use { env } " )
281326 config = Config ()
282327
283328 # Try to load the environment (handles both built-in and custom)
@@ -384,7 +429,7 @@ def reset(
384429 config .set_base_url (Config .DEFAULT_BASE_URL )
385430 config .set_frontend_url (Config .DEFAULT_FRONTEND_URL )
386431 config .set_ssh_key_path (Config .DEFAULT_SSH_KEY_PATH )
387- config .set_current_environment ( "production" )
432+ config .use_production_environment ( )
388433 console .print ("[green]Configuration reset to defaults![/green]" )
389434
390435
0 commit comments