refactor: move selected account logic into function

This commit is contained in:
Carson McManus 2022-02-22 09:17:04 -05:00
parent 10d97efe16
commit 7dcfbc112f

View file

@ -1,5 +1,5 @@
extern crate rpassword; extern crate rpassword;
use clap::{crate_version, App, Arg, Shell}; use clap::{crate_version, App, Arg, Shell, ArgMatches};
use log::*; use log::*;
use std::str::FromStr; use std::str::FromStr;
use std::{ use std::{
@ -12,6 +12,8 @@ use steamguard::{
SteamGuardAccount, UserLogin, SteamGuardAccount, UserLogin,
}; };
use crate::accountmanager::ManifestAccountLoadError;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]
@ -366,27 +368,7 @@ fn run() -> anyhow::Result<()> {
return Ok(()); return Ok(());
} }
let mut selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>> = vec![]; let mut selected_accounts = get_selected_accounts(&matches, &mut manifest)?;
if matches.is_present("all") {
manifest
.load_accounts()
.expect("Failed to load all requested accounts, aborting");
// manifest.accounts.iter().map(|a| selected_accounts.push(a.b));
for entry in &manifest.entries {
selected_accounts.push(manifest.get_account(&entry.account_name).unwrap().clone());
}
} else {
for entry in &manifest.entries {
if !matches.is_present("username") {
selected_accounts.push(manifest.get_account(&entry.account_name).unwrap().clone());
break;
}
if matches.value_of("username").unwrap() == entry.account_name {
selected_accounts.push(manifest.get_account(&entry.account_name).unwrap().clone());
break;
}
}
}
debug!( debug!(
"selected accounts: {:?}", "selected accounts: {:?}",
@ -547,6 +529,28 @@ fn run() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
fn get_selected_accounts(matches: &ArgMatches, manifest: &mut accountmanager::Manifest) -> anyhow::Result<Vec<Arc<Mutex<SteamGuardAccount>>>, ManifestAccountLoadError> {
let mut selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>> = vec![];
if matches.is_present("all") {
manifest.load_accounts()?;
for entry in &manifest.entries {
selected_accounts.push(manifest.get_account(&entry.account_name).unwrap().clone());
}
} else {
let entry = if matches.is_present("username") {
manifest.get_entry(&matches.value_of("username").unwrap().into())
} else {
manifest.entries.first().ok_or(ManifestAccountLoadError::MissingManifestEntry)
}?;
let account_name = entry.account_name.clone();
let account = manifest.get_or_load_account(&account_name)?;
selected_accounts.push(account);
}
return Ok(selected_accounts);
}
fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> { fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> {
if account.account_name.len() > 0 { if account.account_name.len() > 0 {
println!("Username: {}", account.account_name); println!("Username: {}", account.account_name);