Skip to content

Commit d2f542e

Browse files
committed
fix(cli): suppress browser popup during auth via OPENSHELL_NO_BROWSER env var (#419)
1 parent cd40b34 commit d2f542e

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

crates/openshell-cli/src/auth.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,37 @@ pub async fn browser_auth_flow(gateway_endpoint: &str) -> Result<String> {
108108
gateway_endpoint.to_string(),
109109
));
110110

111+
// Allow suppressing the browser popup via environment variable (useful for
112+
// CI, e2e tests, and headless environments).
113+
let no_browser = std::env::var("OPENSHELL_NO_BROWSER")
114+
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
115+
.unwrap_or(false);
116+
111117
// Prompt the user before opening the browser.
112118
eprintln!(" Confirmation code: {code}");
113119
eprintln!(" Verify this code matches your browser before clicking Connect.");
114120
eprintln!();
115-
eprint!("Press Enter to open the browser for authentication...");
116-
std::io::stderr().flush().ok();
117-
let mut _input = String::new();
118-
std::io::stdin().read_line(&mut _input).ok();
119-
120-
if let Err(e) = open_browser(&auth_url) {
121-
debug!(error = %e, "failed to open browser");
122-
eprintln!("Could not open browser automatically.");
121+
122+
if no_browser {
123+
eprintln!("Browser opening suppressed (OPENSHELL_NO_BROWSER is set).");
123124
eprintln!("Open this URL in your browser:");
124125
eprintln!(" {auth_url}");
125126
eprintln!();
126127
} else {
127-
eprintln!("Browser opened.");
128+
eprint!("Press Enter to open the browser for authentication...");
129+
std::io::stderr().flush().ok();
130+
let mut _input = String::new();
131+
std::io::stdin().read_line(&mut _input).ok();
132+
133+
if let Err(e) = open_browser(&auth_url) {
134+
debug!(error = %e, "failed to open browser");
135+
eprintln!("Could not open browser automatically.");
136+
eprintln!("Open this URL in your browser:");
137+
eprintln!(" {auth_url}");
138+
eprintln!();
139+
} else {
140+
eprintln!("Browser opened.");
141+
}
128142
}
129143

130144
// Wait for the callback or timeout.

e2e/rust/tests/cf_auth_smoke.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ async fn run_isolated(args: &[&str]) -> (String, i32) {
2121
.env("XDG_CONFIG_HOME", tmpdir.path())
2222
.env("HOME", tmpdir.path())
2323
.env_remove("OPENSHELL_GATEWAY")
24-
// `gateway add` may enter the browser auth flow, which prompts on stdin.
25-
// Use a closed stdin so auth is skipped instead of hanging the test.
24+
// Suppress browser popup during auth flow.
25+
.env("OPENSHELL_NO_BROWSER", "1")
26+
// Use a closed stdin so auth prompts don't hang the test.
2627
.stdin(Stdio::null())
2728
.stdout(Stdio::piped())
2829
.stderr(Stdio::piped());
@@ -43,8 +44,9 @@ async fn run_with_config(tmpdir: &std::path::Path, args: &[&str]) -> (String, i3
4344
.env("XDG_CONFIG_HOME", tmpdir)
4445
.env("HOME", tmpdir)
4546
.env_remove("OPENSHELL_GATEWAY")
46-
// `gateway add` may enter the browser auth flow, which prompts on stdin.
47-
// Use a closed stdin so auth is skipped instead of hanging the test.
47+
// Suppress browser popup during auth flow.
48+
.env("OPENSHELL_NO_BROWSER", "1")
49+
// Use a closed stdin so auth prompts don't hang the test.
4850
.stdin(Stdio::null())
4951
.stdout(Stdio::piped())
5052
.stderr(Stdio::piped());

0 commit comments

Comments
 (0)