Skip to content

Commit 3a62471

Browse files
committed
fix(cli): warn when env gateway overrides selection
Closes #1206 Warn when gateway select saves a new active gateway while OPENSHELL_GATEWAY is set to a different value, and document the override precedence. Signed-off-by: John Myers <9696606+johntmyers@users.noreply.github.com>
1 parent 689835c commit 3a62471

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

crates/openshell-cli/src/run.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,23 @@ pub fn gateway_use(name: &str) -> Result<()> {
766766

767767
save_active_gateway(name)?;
768768
eprintln!("{} Active gateway set to '{name}'", "✓".green().bold());
769+
if let Some(warning) = gateway_env_override_warning(name) {
770+
eprintln!("{} {warning}", "⚠".yellow().bold());
771+
}
769772
Ok(())
770773
}
771774

775+
fn gateway_env_override_warning(selected_name: &str) -> Option<String> {
776+
let env_name = std::env::var("OPENSHELL_GATEWAY").ok()?;
777+
if env_name.is_empty() || env_name == selected_name {
778+
return None;
779+
}
780+
781+
Some(format!(
782+
"OPENSHELL_GATEWAY={env_name} is set and will override this selection.\n Unset it or run: export OPENSHELL_GATEWAY={selected_name}"
783+
))
784+
}
785+
772786
pub fn gateway_select(name: Option<&str>, gateway_flag: &Option<String>) -> Result<()> {
773787
let interactive = std::io::stdin().is_terminal() && std::io::stdout().is_terminal();
774788
gateway_select_with(name, gateway_flag, interactive, |gateways, default| {
@@ -5971,8 +5985,8 @@ mod tests {
59715985
use super::{
59725986
GatewayControlTarget, TlsOptions, dockerfile_sources_supported_for_gateway,
59735987
format_gateway_select_header, format_gateway_select_items, gateway_add, gateway_auth_label,
5974-
gateway_select_with, gateway_type_label, git_sync_files, http_health_check,
5975-
image_requests_gpu, inferred_provider_type, parse_cli_setting_value,
5988+
gateway_env_override_warning, gateway_select_with, gateway_type_label, git_sync_files,
5989+
http_health_check, image_requests_gpu, inferred_provider_type, parse_cli_setting_value,
59765990
parse_credential_pairs, plaintext_gateway_is_remote, provisioning_timeout_message,
59775991
ready_false_condition_message, resolve_from, resolve_gateway_control_target_from,
59785992
sandbox_should_persist, shell_escape, source_requests_gpu, validate_gateway_name,
@@ -6514,6 +6528,35 @@ mod tests {
65146528
});
65156529
}
65166530

6531+
#[test]
6532+
fn gateway_env_override_warning_mentions_masked_selection() {
6533+
let _guard = TEST_ENV_LOCK
6534+
.lock()
6535+
.unwrap_or_else(std::sync::PoisonError::into_inner);
6536+
let _env = EnvVarGuard::set("OPENSHELL_GATEWAY", "openshell");
6537+
6538+
let warning = gateway_env_override_warning("docker-dev").expect("env override should warn");
6539+
6540+
assert!(
6541+
warning.contains("OPENSHELL_GATEWAY=openshell"),
6542+
"warning should name the overriding env var: {warning}"
6543+
);
6544+
assert!(
6545+
warning.contains("export OPENSHELL_GATEWAY=docker-dev"),
6546+
"warning should suggest updating the env var: {warning}"
6547+
);
6548+
}
6549+
6550+
#[test]
6551+
fn gateway_env_override_warning_skips_matching_gateway() {
6552+
let _guard = TEST_ENV_LOCK
6553+
.lock()
6554+
.unwrap_or_else(std::sync::PoisonError::into_inner);
6555+
let _env = EnvVarGuard::set("OPENSHELL_GATEWAY", "docker-dev");
6556+
6557+
assert_eq!(gateway_env_override_warning("docker-dev"), None);
6558+
}
6559+
65176560
#[test]
65186561
fn gateway_select_prefers_active_gateway_as_default_choice() {
65196562
let tmpdir = tempfile::tempdir().expect("create tmpdir");

docs/sandboxes/manage-gateways.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ For direct mTLS endpoints, place the CLI client certificate bundle in the gatewa
158158

159159
One gateway is always the active gateway. All CLI commands target it by default. `gateway add` sets the new gateway as active.
160160

161+
The active gateway is the persisted default. The `-g` flag and the `OPENSHELL_GATEWAY` environment variable override it when commands resolve a gateway. If `OPENSHELL_GATEWAY` is set to a different gateway, `openshell gateway select <name>` still saves the new default and warns that the current shell will keep using the environment value until you unset or update it.
162+
161163
List all registered gateways:
162164

163165
```shell

0 commit comments

Comments
 (0)