Skip to content

Commit 8bdde60

Browse files
authored
Merge branch 'master' into assetCatalogParsing
2 parents 2627c94 + 700959e commit 8bdde60

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/commands/login.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
104104
Config::from_cli_config()?
105105
};
106106

107+
if should_warn_about_overwrite(config_to_update.get_auth(), &token) {
108+
println!();
109+
println!("Warning: You are about to overwrite an existing token!");
110+
111+
// Show organization information
112+
if let Some(existing_auth) = config_to_update.get_auth() {
113+
let existing_org = get_org_from_auth(existing_auth);
114+
let new_org = get_org_from_token(&token);
115+
116+
println!("The current token is {}.", format_org_info(existing_org));
117+
println!("The new token is {}.", format_org_info(new_org));
118+
}
119+
120+
println!();
121+
if !prompt_to_continue("Do you want to continue and overwrite the existing token?")? {
122+
println!("Token update cancelled.");
123+
return Ok(());
124+
}
125+
}
126+
107127
update_config(&config_to_update, token)?;
108128
println!();
109129
println!(
@@ -113,3 +133,37 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
113133

114134
Ok(())
115135
}
136+
137+
/// Helper function to get organization name from auth (treats keys and tokens without org as "(unknown)")
138+
fn get_org_from_auth(auth: &Auth) -> Option<&str> {
139+
match auth {
140+
Auth::Token(token) => get_org_from_token(token),
141+
Auth::Key(_) => None,
142+
}
143+
}
144+
145+
/// Helper function to get organization name from token
146+
fn get_org_from_token(token: &AuthToken) -> Option<&str> {
147+
token.payload().map(|p| p.org.as_str())
148+
}
149+
150+
/// Helper function to format organization information for display
151+
fn format_org_info(org: Option<&str>) -> String {
152+
match org {
153+
Some(org_name) => format!("for organization {}", org_name),
154+
None => "not tied to any specific organization".to_string(),
155+
}
156+
}
157+
158+
/// Helper function to determine if we should warn about overwriting an existing token
159+
fn should_warn_about_overwrite(existing_auth: Option<&Auth>, new_token: &AuthToken) -> bool {
160+
// Only warn if there's an existing auth
161+
let Some(existing_auth) = existing_auth else {
162+
return false;
163+
};
164+
165+
let existing_org = get_org_from_auth(existing_auth);
166+
let new_org = get_org_from_token(new_token);
167+
168+
existing_org != new_org
169+
}

0 commit comments

Comments
 (0)