diff --git a/src/browser.rs b/src/browser.rs new file mode 100644 index 000000000..d3e2d3e25 --- /dev/null +++ b/src/browser.rs @@ -0,0 +1,23 @@ +use crate::print::{self}; + +pub fn open_link(link: &str, success_prompt: Option<&str>, error_prompt: Option<&str>) -> bool { + match open::that(&link) { + Ok(_) => { + print::success_msg("Opening URL in browser", link); + match success_prompt { + Some(prompt) => print::prompt(prompt), + None => (), + } + true + } + Err(e) => { + print::error!("Cannot launch browser: {e:#}"); + print::prompt(match error_prompt { + Some(prompt) => prompt, + None => "Please paste the URL below into your browser:", + }); + println!("{link}"); + false + } + } +} diff --git a/src/cloud/auth.rs b/src/cloud/auth.rs index 8909ad272..bdaf127b7 100644 --- a/src/cloud/auth.rs +++ b/src/cloud/auth.rs @@ -8,6 +8,7 @@ use fs_err as fs; use tokio::time::sleep; use crate::branding::BRANDING_CLOUD; +use crate::browser::open_link; use crate::cloud::client::{ cloud_config_dir, cloud_config_file, CloudClient, CloudConfig, ErrorResponse, }; @@ -78,13 +79,11 @@ pub async fn _do_login(client: &mut CloudClient) -> anyhow::Result<()> { } = client .post("auth/sessions/", &HashMap::from([("type", "CLI")])) .await?; - let link = client.api_endpoint.join(&auth_url)?.to_string(); - log::debug!("Opening URL in browser: {}", link); - if open::that(&link).is_ok() { - print::prompt("Page to complete authentication now open in your browser."); - } else { - print::prompt("Please paste this link into your browser to complete authentication:"); - print::success_msg("Link", link); + { + let link = client.api_endpoint.join(&auth_url)?.to_string(); + let success_prompt = "Complete the authentication process now open in your browser"; + let error_prompt = "Please paste this link into your browser to complete authentication:"; + open_link(&link, Some(&success_prompt), Some(&error_prompt)); } let deadline = Instant::now() + AUTHENTICATION_WAIT_TIME; while Instant::now() < deadline { diff --git a/src/commands/ui.rs b/src/commands/ui.rs index be0b00aca..e66194de9 100644 --- a/src/commands/ui.rs +++ b/src/commands/ui.rs @@ -3,6 +3,7 @@ use std::io::{stdout, Write}; use anyhow::Context; use crate::branding::{BRANDING, BRANDING_CLI_CMD}; +use crate::browser::open_link; use crate::cloud; use crate::commands::ExitCode; use crate::options::{Options, UI}; @@ -29,20 +30,11 @@ pub fn show_ui(cmd: &UI, opts: &Options) -> anyhow::Result<()> { .expect("stdout write succeeds"); Ok(()) } else { - match open::that(&url) { - Ok(_) => { - print::success!("Opening URL in browser:"); - println!("{url}"); - Ok(()) - } - Err(e) => { - print::error!("Cannot launch browser: {e:#}"); - print::prompt( - "Please paste the URL below into your browser to launch the {BRANDING} UI:", - ); - println!("{url}"); - Err(ExitCode::new(1).into()) - } + let error_prompt = + format!("Please paste the URL below into your browser to launch the {BRANDING} UI:"); + match open_link(&url, None, Some(&error_prompt)) { + true => Ok(()), + false => Err(ExitCode::new(1).into()), } } } diff --git a/src/main.rs b/src/main.rs index ad4f96fb0..061326e1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod analyze; mod async_util; mod branch; mod branding; +mod browser; mod bug; mod classify; pub(crate) mod cli;