Skip to content

Commit

Permalink
Combine link opening code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwpark committed Nov 29, 2024
1 parent e80a37d commit ac21eee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
23 changes: 23 additions & 0 deletions src/browser.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
13 changes: 6 additions & 7 deletions src/cloud/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 authentication 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 {
Expand Down
20 changes: 6 additions & 14 deletions src/commands/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod analyze;
mod async_util;
mod branch;
mod branding;
mod browser;
mod bug;
mod classify;
pub(crate) mod cli;
Expand Down

0 comments on commit ac21eee

Please sign in to comment.