Skip to content

Commit

Permalink
Prompt the user to open the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
sudomateo committed Oct 24, 2023
1 parent de07bd6 commit 957e117
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions cli/src/cmd_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

// Copyright 2023 Oxide Computer Company

use std::{collections::HashMap, fs::File, io::Read};
use std::{
collections::HashMap,
fs::File,
io::{self, stdin, BufRead, BufReader, Read, Write},
};

use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -243,21 +247,28 @@ impl CmdAuthLogin {

let uri = details.verification_uri().to_string();

println!(
"Copy your one-time code:\n {}",
details.user_code().secret()
);

let opened = match (&self.browser, self.no_browser) {
(None, false) => open::that(&uri).is_ok(),
(Some(app), false) => open::with(&uri, app).is_ok(),
(None, false) => {
wait_for_enter(&uri)?;
open::that(&uri).is_ok()
}
(Some(app), false) => {
wait_for_enter(&uri)?;
open::with(&uri, app).is_ok()
}
(None, true) => false,
(Some(_), true) => unreachable!(),
};

if opened {
println!("Opened this URL in your browser:\n {}", uri);
} else {
if !opened {
println!("Open this URL in your browser:\n {}", uri);
}

println!("\nEnter the code: {}\n", details.user_code().secret());

auth_client
.exchange_device_access_token(&details)
.request_async(async_http_client_custom, tokio::time::sleep, None)
Expand Down Expand Up @@ -329,6 +340,14 @@ impl CmdAuthLogin {
}
}

/// Wait for the user to press the enter key to continue.
fn wait_for_enter(uri: &str) -> Result<usize> {
print!("Press ENTER to open {} in your browser...", &uri);
io::stdout().flush()?;
let mut buf = String::new();
Ok(BufReader::new(stdin()).read_line(&mut buf)?)
}

/// Removes saved authentication information.
///
/// This command does not invalidate any tokens from the hosts.
Expand Down

0 comments on commit 957e117

Please sign in to comment.