Skip to content

Commit 5840b72

Browse files
committed
feat(login): add query params to q login cmd with pro license
1 parent fc510b8 commit 5840b72

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

crates/q_cli/src/cli/installation.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ use fig_util::{
2323
};
2424
use tracing::warn;
2525

26-
use super::user::login_interactive;
26+
use super::user::{
27+
LoginArgs,
28+
login_interactive,
29+
};
2730
use crate::util::choose;
2831

2932
#[cfg_attr(windows, allow(unused_variables))]
@@ -139,7 +142,12 @@ pub async fn install_cli(
139142
eyre::bail!("You must run with --no-confirm if unattended");
140143
}
141144

142-
login_interactive().await?;
145+
login_interactive(LoginArgs {
146+
license: None,
147+
identity_provider: None,
148+
region: None,
149+
})
150+
.await?;
143151
} else {
144152
println!();
145153
println!("You must login before you can use {PRODUCT_NAME}'s features.");

crates/q_cli/src/cli/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl CliRootCommands {
221221
CliRootCommands::Init(_) => "init",
222222
CliRootCommands::Theme(_) => "theme",
223223
CliRootCommands::Issue(_) => "issue",
224-
CliRootCommands::RootUser(RootUserSubcommand::Login) => "login",
224+
CliRootCommands::RootUser(RootUserSubcommand::Login(_)) => "login",
225225
CliRootCommands::RootUser(RootUserSubcommand::Logout) => "logout",
226226
CliRootCommands::RootUser(RootUserSubcommand::Whoami { .. }) => "whoami",
227227
CliRootCommands::User(_) => "user",

crates/q_cli/src/cli/user.rs

+61-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::process::{
77
use std::time::Duration;
88

99
use anstream::println;
10-
use clap::Subcommand;
10+
use clap::{
11+
Args,
12+
Subcommand,
13+
};
1114
use crossterm::style::Stylize;
1215
use eyre::{
1316
Result,
@@ -46,7 +49,7 @@ use crate::util::{
4649
#[derive(Subcommand, Debug, PartialEq, Eq)]
4750
pub enum RootUserSubcommand {
4851
/// Login
49-
Login,
52+
Login(LoginArgs),
5053
/// Logout
5154
Logout,
5255
/// Prints details about the current user
@@ -57,6 +60,29 @@ pub enum RootUserSubcommand {
5760
},
5861
}
5962

63+
#[derive(Args, Debug, PartialEq, Eq, Clone)]
64+
pub struct LoginArgs {
65+
/// License type (pro for Identity Center, free for Builder ID)
66+
#[arg(long, value_enum)]
67+
pub license: Option<LicenseType>,
68+
69+
/// Identity provider URL (for Identity Center)
70+
#[arg(long)]
71+
pub identity_provider: Option<String>,
72+
73+
/// Region (for Identity Center)
74+
#[arg(long)]
75+
pub region: Option<String>,
76+
}
77+
78+
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
79+
pub enum LicenseType {
80+
/// Free license with Builder ID
81+
Free,
82+
/// Pro license with Identity Center
83+
Pro,
84+
}
85+
6086
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6187
enum AuthMethod {
6288
/// Builder ID (free)
@@ -77,15 +103,15 @@ impl Display for AuthMethod {
77103
impl RootUserSubcommand {
78104
pub async fn execute(self) -> Result<ExitCode> {
79105
match self {
80-
Self::Login => {
106+
Self::Login(args) => {
81107
if fig_auth::is_logged_in().await {
82108
eyre::bail!(
83109
"Already logged in, please logout with {} first",
84110
format!("{CLI_BINARY_NAME} logout").magenta()
85111
);
86112
}
87113

88-
login_interactive().await?;
114+
login_interactive(args).await?;
89115

90116
Ok(ExitCode::SUCCESS)
91117
},
@@ -155,23 +181,44 @@ impl UserSubcommand {
155181
}
156182
}
157183

158-
pub async fn login_interactive() -> Result<()> {
159-
let options = [AuthMethod::BuilderId, AuthMethod::IdentityCenter];
160-
let i = match choose("Select login method", &options)? {
161-
Some(i) => i,
162-
None => bail!("No login method selected"),
184+
pub async fn login_interactive(args: LoginArgs) -> Result<()> {
185+
let login_method = match args.license {
186+
Some(LicenseType::Free) => AuthMethod::BuilderId,
187+
Some(LicenseType::Pro) => AuthMethod::IdentityCenter,
188+
None => {
189+
// No license specified, prompt the user to choose
190+
let options = [AuthMethod::BuilderId, AuthMethod::IdentityCenter];
191+
let i = match choose("Select login method", &options)? {
192+
Some(i) => i,
193+
None => bail!("No login method selected"),
194+
};
195+
options[i]
196+
},
163197
};
164-
let login_method = options[i];
198+
165199
match login_method {
166200
AuthMethod::BuilderId | AuthMethod::IdentityCenter => {
167201
let (start_url, region) = match login_method {
168202
AuthMethod::BuilderId => (None, None),
169203
AuthMethod::IdentityCenter => {
170-
let default_start_url = fig_settings::state::get_string("auth.idc.start-url").ok().flatten();
171-
let default_region = fig_settings::state::get_string("auth.idc.region").ok().flatten();
204+
let default_start_url = args
205+
.identity_provider
206+
.or_else(|| fig_settings::state::get_string("auth.idc.start-url").ok().flatten());
207+
let default_region = args
208+
.region
209+
.or_else(|| fig_settings::state::get_string("auth.idc.region").ok().flatten());
210+
211+
let start_url = if let Some(url) = default_start_url.clone() {
212+
url
213+
} else {
214+
input("Enter Start URL", default_start_url.as_deref())?
215+
};
172216

173-
let start_url = input("Enter Start URL", default_start_url.as_deref())?;
174-
let region = input("Enter Region", default_region.as_deref())?;
217+
let region = if let Some(reg) = default_region.clone() {
218+
reg
219+
} else {
220+
input("Enter Region", default_region.as_deref())?
221+
};
175222

176223
let _ = fig_settings::state::set_value("auth.idc.start-url", start_url.clone());
177224
let _ = fig_settings::state::set_value("auth.idc.region", region.clone());

0 commit comments

Comments
 (0)