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
19 changes: 15 additions & 4 deletions crates/q_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mod translate;
mod uninstall;
mod update;
mod user;

use std::io::{
Write as _,
stdout,
Expand Down Expand Up @@ -87,7 +86,7 @@ use crate::util::desktop::{
};
use crate::util::{
CliContext,
assert_logged_in,
is_logged_in_check,
};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
Expand Down Expand Up @@ -397,8 +396,20 @@ impl Cli {
}

pub async fn execute_chat(subcmd: &str, args: Option<Vec<String>>, enforce_login: bool) -> Result<ExitCode> {
if enforce_login {
assert_logged_in().await?;
if enforce_login && !is_logged_in_check().await {
if subcmd == "chat" {
let options = ["Yes", "No"];
match crate::util::choose(" You are not logged in. Login now?", &options)? {
Some(0) => {},
_ => bail!("Login is required to use chat"),
}
crate::cli::user::login_interactive(Default::default()).await?;
} else {
bail!(
"You are not logged in, please log in with {}",
format!("{CLI_BINARY_NAME} login",).bold()
);
}
}

// Save credentials from the macOS keychain to sqlite.
Expand Down
6 changes: 5 additions & 1 deletion crates/q_cli/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,12 @@ pub fn dialoguer_theme() -> ColorfulTheme {
}
}

pub async fn is_logged_in_check() -> bool {
std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) || fig_auth::is_logged_in().await
}

pub async fn assert_logged_in() -> Result<(), Error> {
if !(std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) || fig_auth::is_logged_in().await) {
if !is_logged_in_check().await {
bail!(
"You are not logged in, please log in with {}",
format!("{CLI_BINARY_NAME} login",).bold()
Expand Down
Loading