Skip to content

Commit e86e1f3

Browse files
authored
feat(auth): interactive login if logged out for q or q chat (#1817)
1 parent 34afb2c commit e86e1f3

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

crates/q_cli/src/cli/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ use tracing::{
7474

7575
use self::integrations::IntegrationsSubcommands;
7676
use self::user::RootUserSubcommand;
77-
use crate::util::CliContext;
7877
use crate::util::desktop::{
7978
LaunchArgs,
8079
launch_fig_desktop,
8180
};
81+
use crate::util::{
82+
CliContext,
83+
assert_logged_in,
84+
};
8285

8386
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
8487
pub enum OutputFormat {
@@ -350,6 +353,8 @@ impl Cli {
350353
}
351354

352355
async fn execute_chat(args: Option<Vec<String>>) -> Result<ExitCode> {
356+
assert_logged_in().await?;
357+
353358
let secret_store = SecretStore::new().await.ok();
354359
if let Some(secret_store) = secret_store {
355360
if let Ok(database) = database() {

crates/q_cli/src/cli/user.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use crate::util::spinner::{
5959
SpinnerComponent,
6060
};
6161
use crate::util::{
62+
assert_logged_in,
6263
choose,
6364
input,
6465
};
@@ -198,12 +199,7 @@ impl RootUserSubcommand {
198199
}
199200
},
200201
Self::Profile => {
201-
if !fig_util::system_info::in_cloudshell() && !fig_auth::is_logged_in().await {
202-
bail!(
203-
"You are not logged in, please log in with {}",
204-
format!("{CLI_BINARY_NAME} login",).bold()
205-
);
206-
}
202+
assert_logged_in().await?;
207203

208204
if let Ok(Some(token)) = fig_auth::builder_id_token().await {
209205
if matches!(token.token_type(), TokenType::BuilderId) {

crates/q_cli/src/util/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use dialoguer::theme::ColorfulTheme;
3232
use eyre::{
3333
Context,
3434
ContextCompat,
35+
Error,
3536
Result,
3637
bail,
3738
};
@@ -289,6 +290,17 @@ pub fn dialoguer_theme() -> ColorfulTheme {
289290
}
290291
}
291292

293+
pub async fn assert_logged_in() -> Result<(), Error> {
294+
if !fig_util::system_info::in_cloudshell() && !fig_auth::is_logged_in().await {
295+
bail!(
296+
"You are not logged in, please log in with {}",
297+
format!("{CLI_BINARY_NAME} login",).bold()
298+
);
299+
}
300+
301+
Ok(())
302+
}
303+
292304
#[cfg(target_os = "macos")]
293305
pub async fn is_brew_reinstall() -> bool {
294306
let regex = regex::bytes::Regex::new(r"brew(\.\w+)?\s+(upgrade|reinstall|install)").unwrap();

0 commit comments

Comments
 (0)