Skip to content
Closed
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
16 changes: 15 additions & 1 deletion crates/chat-cli/src/cli/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ impl LoginArgs {
} else {
// --license is not specified, prompt the user to choose
let options = [AuthMethod::BuilderId, AuthMethod::IdentityCenter];
let i = match choose("Select login method", &options)? {
let default_idx = os.database.get_login_method()?
.and_then(|method| {
if method == "BuilderId" { Some(0) }
else if method == "IdentityCenter" { Some(1) }
else { None }
})
.unwrap_or(0);
let i = match choose("Select login method", &options, default_idx)? {
Some(i) => i,
None => bail!("No login method selected"),
};
Expand Down Expand Up @@ -165,6 +172,13 @@ impl LoginArgs {
},
};

// Save the login method for next time
let method_str = match login_method {
AuthMethod::BuilderId => "BuilderId",
AuthMethod::IdentityCenter => "IdentityCenter",
};
let _ = os.database.set_login_method(method_str.to_string());

if login_method == AuthMethod::IdentityCenter {
select_profile_interactive(os, true).await?;
}
Expand Down
11 changes: 11 additions & 0 deletions crates/chat-cli/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const CLIENT_ID_KEY: &str = "telemetryClientId";
const CODEWHISPERER_PROFILE_KEY: &str = "api.codewhisperer.profile";
const START_URL_KEY: &str = "auth.idc.start-url";
const IDC_REGION_KEY: &str = "auth.idc.region";
const LOGIN_METHOD_KEY: &str = "auth.login-method";
// We include this key to remove for backwards compatibility
const CUSTOMIZATION_STATE_KEY: &str = "api.selectedCustomization";
const PROFILE_MIGRATION_KEY: &str = "profile.Migrated";
Expand Down Expand Up @@ -324,6 +325,16 @@ impl Database {
self.set_json_entry(Table::State, IDC_REGION_KEY, region)
}

/// Get the last used login method.
pub fn get_login_method(&self) -> Result<Option<String>, DatabaseError> {
self.get_json_entry::<String>(Table::State, LOGIN_METHOD_KEY)
}

/// Set the last used login method.
pub fn set_login_method(&mut self, method: String) -> Result<usize, DatabaseError> {
self.set_json_entry(Table::State, LOGIN_METHOD_KEY, method)
}

/// Get if user has already completed a migration
pub fn get_has_migrated(&self) -> Result<Option<bool>, DatabaseError> {
self.get_entry::<bool>(Table::State, PROFILE_MIGRATION_KEY)
Expand Down
4 changes: 2 additions & 2 deletions crates/chat-cli/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl std::fmt::Display for UnknownDesktopErrContext {
}
}

pub fn choose(prompt: impl Display, options: &[impl ToString]) -> Result<Option<usize>> {
pub fn choose(prompt: impl Display, options: &[impl ToString], default: usize) -> Result<Option<usize>> {
if options.is_empty() {
bail!("no options passed to choose")
}
Expand All @@ -71,7 +71,7 @@ pub fn choose(prompt: impl Display, options: &[impl ToString]) -> Result<Option<

match Select::with_theme(&dialoguer_theme())
.items(options)
.default(0)
.default(default)
.with_prompt(prompt.to_string())
.interact_opt()
{
Expand Down