diff --git a/src/accountmanager.rs b/src/accountmanager.rs index 6105a96..f5b207c 100644 --- a/src/accountmanager.rs +++ b/src/accountmanager.rs @@ -98,6 +98,19 @@ impl Manifest { self.accounts.push(Arc::new(Mutex::new(account))); } + pub fn import_account(&mut self, import_path: String) -> anyhow::Result<()> { + let path = Path::new(&import_path); + ensure!(path.exists(), "{} does not exist.", import_path); + ensure!(path.is_file(), "{} is not a file.", import_path); + + let file = File::open(path)?; + let reader = BufReader::new(file); + let account: SteamGuardAccount = serde_json::from_reader(reader)?; + self.add_account(account); + + return Ok(()); + } + pub fn remove_account(&mut self, account_name: String) { let index = self .accounts diff --git a/src/main.rs b/src/main.rs index ae7cecd..96895ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,6 +88,15 @@ fn main() { App::new("setup") .about("Set up a new account with steamguard-cli") ) + .subcommand( + App::new("import") + .about("Import an account with steamguard already set up") + .arg( + Arg::with_name("files") + .required(true) + .multiple(true) + ) + ) .subcommand( App::new("remove") .about("Remove the authenticator from an account.") @@ -252,6 +261,20 @@ fn main() { } } + return; + } else if let Some(import_matches) = matches.subcommand_matches("import") { + for file_path in import_matches.values_of("files").unwrap() { + match manifest.import_account(file_path.into()) { + Ok(_) => { + info!("Imported account: {}", file_path); + } + Err(err) => { + error!("Failed to import account: {} {}", file_path, err); + } + } + } + + manifest.save().expect("Failed to save manifest."); return; }