don't allow setup or import for account_names already in the manifest, fixes #109

This commit is contained in:
Carson McManus 2021-09-06 16:51:44 -04:00
parent a92cd5b1aa
commit e4cf29e852
2 changed files with 17 additions and 4 deletions

View file

@ -116,6 +116,15 @@ impl Manifest {
Ok(()) Ok(())
} }
pub fn account_exists(&self, account_name: &String) -> bool {
for entry in &self.entries {
if &entry.account_name == account_name {
return true;
}
}
return false;
}
pub fn add_account(&mut self, account: SteamGuardAccount) { pub fn add_account(&mut self, account: SteamGuardAccount) {
debug!("adding account to manifest: {}", account.account_name); debug!("adding account to manifest: {}", account.account_name);
let steamid = account.session.as_ref().map_or(0, |s| s.steam_id); let steamid = account.session.as_ref().map_or(0, |s| s.steam_id);
@ -136,6 +145,7 @@ impl Manifest {
let file = File::open(path)?; let file = File::open(path)?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let account: SteamGuardAccount = serde_json::from_reader(reader)?; let account: SteamGuardAccount = serde_json::from_reader(reader)?;
ensure!(!self.account_exists(&account.account_name), "Account already exists in manifest, please remove it first.");
self.add_account(account); self.add_account(account);
return Ok(()); return Ok(());

View file

@ -210,7 +210,12 @@ fn main() {
if matches.is_present("setup") { if matches.is_present("setup") {
println!("Log in to the account that you want to link to steamguard-cli"); println!("Log in to the account that you want to link to steamguard-cli");
let session = do_login_raw().expect("Failed to log in. Account has not been linked."); print!("Username: ");
let username = tui::prompt();
if manifest.account_exists(&username) {
error!("Account {} already exists in manifest, remove it first", username);
}
let session = do_login_raw(username).expect("Failed to log in. Account has not been linked.");
let mut linker = AccountLinker::new(session); let mut linker = AccountLinker::new(session);
let account: SteamGuardAccount; let account: SteamGuardAccount;
@ -515,9 +520,7 @@ fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> {
return Ok(()); return Ok(());
} }
fn do_login_raw() -> anyhow::Result<steamapi::Session> { fn do_login_raw(username: String) -> anyhow::Result<steamapi::Session> {
print!("Username: ");
let username = tui::prompt();
let _ = std::io::stdout().flush(); let _ = std::io::stdout().flush();
let password = rpassword::prompt_password_stdout("Password: ").unwrap(); let password = rpassword::prompt_password_stdout("Password: ").unwrap();
if password.len() > 0 { if password.len() > 0 {