Skip to content

Commit 80dfacf

Browse files
committed
enfore login when chat
1 parent a432055 commit 80dfacf

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

crates/chat-cli/src/cli/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,23 @@ impl RootSubcommand {
123123
matches!(self, Self::Chat(_) | Self::Login(_) | Self::Profile | Self::Issue(_))
124124
}
125125

126-
pub fn requires_auth(&self) -> bool {
127-
matches!(self, Self::Chat(_) | Self::Profile)
128-
}
129-
130126
pub async fn execute(self, os: &mut Os) -> Result<ExitCode> {
131127
// Check for auth on subcommands that require it.
132-
if self.requires_auth() && !is_logged_in(&mut os.database).await {
133-
bail!(
134-
"You are not logged in, please log in with {}",
135-
format!("{CLI_BINARY_NAME} login").bold()
136-
);
128+
if !is_logged_in(&mut os.database).await {
129+
if matches!(self, Self::Chat(_)) {
130+
let options = ["Yes", "No"];
131+
match crate::util::choose(" You are not logged in. Login now?", &options)? {
132+
Some(0) => {},
133+
_ => bail!("Login is required to use chat"),
134+
}
135+
136+
LoginArgs::default().execute(os).await?;
137+
} else if matches!(self, Self::Profile) {
138+
bail!(
139+
"You are not logged in, please log in with {}",
140+
format!("{CLI_BINARY_NAME} login").bold()
141+
);
142+
}
137143
}
138144

139145
// Send executed telemetry.

crates/q_cli/src/cli/mod.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ mod translate;
2020
mod uninstall;
2121
mod update;
2222
mod user;
23-
2423
use std::io::{
2524
Write as _,
2625
stdout,
@@ -87,7 +86,7 @@ use crate::util::desktop::{
8786
};
8887
use crate::util::{
8988
CliContext,
90-
assert_logged_in,
89+
is_logged_in_check,
9190
};
9291

9392
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
@@ -375,30 +374,23 @@ impl Cli {
375374

376375
Self::execute_chat("chat", Some(args), true).await
377376
},
378-
CliRootCommands::Mcp { args } => {
379-
if args.iter().any(|arg| ["--help", "-h"].contains(&arg.as_str())) {
380-
return Self::execute_chat("mcp", Some(args), false).await;
381-
}
382-
383-
Self::execute_chat("mcp", Some(args), true).await
384-
},
377+
CliRootCommands::Mcp { args } => Self::execute_chat("mcp", Some(args), false).await,
385378
CliRootCommands::Inline(subcommand) => subcommand.execute(&cli_context).await,
386-
CliRootCommands::Agent { args } => {
387-
if args.iter().any(|arg| ["--help", "-h"].contains(&arg.as_str())) {
388-
return Self::execute_chat("agent", Some(args), false).await;
389-
}
390-
391-
Self::execute_chat("agent", Some(args), true).await
392-
},
379+
CliRootCommands::Agent { args } => Self::execute_chat("agent", Some(args), false).await,
393380
},
394381
// Root command
395382
None => Self::execute_chat("chat", None, true).await,
396383
}
397384
}
398385

399386
pub async fn execute_chat(subcmd: &str, args: Option<Vec<String>>, enforce_login: bool) -> Result<ExitCode> {
400-
if enforce_login {
401-
assert_logged_in().await?;
387+
if enforce_login && !is_logged_in_check().await {
388+
let options = ["Yes", "No"];
389+
match crate::util::choose(" You are not logged in. Login now?", &options)? {
390+
Some(0) => {},
391+
_ => bail!("Login is required to use chat"),
392+
}
393+
crate::cli::user::login_interactive(Default::default()).await?;
402394
}
403395

404396
// Save credentials from the macOS keychain to sqlite.

crates/q_cli/src/util/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,12 @@ pub fn dialoguer_theme() -> ColorfulTheme {
290290
}
291291
}
292292

293+
pub async fn is_logged_in_check() -> bool {
294+
std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) || fig_auth::is_logged_in().await
295+
}
296+
293297
pub async fn assert_logged_in() -> Result<(), Error> {
294-
if !(std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) || fig_auth::is_logged_in().await) {
298+
if !is_logged_in_check().await {
295299
bail!(
296300
"You are not logged in, please log in with {}",
297301
format!("{CLI_BINARY_NAME} login",).bold()

0 commit comments

Comments
 (0)