From 7dcfbc112fa0301ed4db9027b2545baf8a007bdc Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 22 Feb 2022 09:17:04 -0500 Subject: [PATCH] refactor: move selected account logic into function --- src/main.rs | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0e3138e..6b916cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ extern crate rpassword; -use clap::{crate_version, App, Arg, Shell}; +use clap::{crate_version, App, Arg, Shell, ArgMatches}; use log::*; use std::str::FromStr; use std::{ @@ -12,6 +12,8 @@ use steamguard::{ SteamGuardAccount, UserLogin, }; +use crate::accountmanager::ManifestAccountLoadError; + #[macro_use] extern crate lazy_static; #[macro_use] @@ -366,27 +368,7 @@ fn run() -> anyhow::Result<()> { return Ok(()); } - let mut selected_accounts: Vec>> = vec![]; - 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; - } - } - } + let mut selected_accounts = get_selected_accounts(&matches, &mut manifest)?; debug!( "selected accounts: {:?}", @@ -547,6 +529,28 @@ fn run() -> anyhow::Result<()> { Ok(()) } +fn get_selected_accounts(matches: &ArgMatches, manifest: &mut accountmanager::Manifest) -> anyhow::Result>>, ManifestAccountLoadError> { + let mut selected_accounts: Vec>> = 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<()> { if account.account_name.len() > 0 { println!("Username: {}", account.account_name);