Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/tower-cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl App {
}

match matches.subcommand() {
Some(("login", _)) => session::do_login(config).await,
Some(("login", args)) => session::do_login(config, args).await,
Some(("version", _)) => version::do_version().await,
Some(("apps", sub_matches)) => {
let apps_command = sub_matches.subcommand();
Expand Down
50 changes: 36 additions & 14 deletions crates/tower-cmd/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::output;
use clap::Command;
use clap::{Arg, ArgMatches, Command};
use config::{Config, Session};
use tokio::{time, time::Duration};
use tower_api::models::CreateDeviceLoginTicketResponse;
Expand All @@ -8,18 +8,29 @@ use tower_telemetry::debug;
use crate::api;

pub fn login_cmd() -> Command {
Command::new("login").about("Create a session with Tower")
Command::new("login")
.arg(
Arg::new("no-browser")
.long("no-browser")
.short('n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to a better idea

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe -f?

.help("Do not attempt to open the browser automatically")
.action(clap::ArgAction::SetTrue),
)
.about("Create a session with Tower")
}

pub async fn do_login(config: Config) {
pub async fn do_login(config: Config, args: &ArgMatches) {
output::banner();

// Open a browser by default, unless the --no-browser flag is set.
let open_browser = !args.get_flag("no-browser");

let mut spinner = output::spinner("Starting device login...");

match api::create_device_login_ticket(&config).await {
Ok(resp) => {
spinner.success();
handle_device_login(config, resp).await;
handle_device_login(config, open_browser, resp).await;
}
Err(err) => {
spinner.failure();
Expand All @@ -28,18 +39,29 @@ pub async fn do_login(config: Config) {
}
}

async fn handle_device_login(config: Config, claim: CreateDeviceLoginTicketResponse) {
async fn handle_device_login(
config: Config,
open_browser: bool,
claim: CreateDeviceLoginTicketResponse,
) {
// Put this in the debug logs just in case something weird happens.
debug!("Login URL: {}", claim.login_url);

let login_instructions = format!(
"Please open the following URL in your browser: {}\n",
claim.login_url
);

// Try to open the login URL in browser
if let Err(err) = webbrowser::open(&claim.login_url) {
debug!("failed to open web browser: {}", err);

let line = format!(
"Please open the following URL in your browser: {}\n",
claim.login_url
);
output::write(&line);
if open_browser {
if let Err(err) = webbrowser::open(&claim.login_url) {
debug!("failed to open web browser: {}", err);
output::write(&login_instructions);
} else {
debug!("opened browser to {}", claim.login_url);
}
} else {
debug!("opened browser to {}", claim.login_url);
output::write(&login_instructions);
}

let mut spinner = output::spinner("Waiting for login...");
Expand Down