Merge pull request #86 from dyc3/account-import

Add the ability to import existing accounts
This commit is contained in:
Carson McManus 2021-08-14 09:22:50 -04:00 committed by GitHub
commit d594da3b86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View file

@ -98,6 +98,19 @@ impl Manifest {
self.accounts.push(Arc::new(Mutex::new(account))); 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) { pub fn remove_account(&mut self, account_name: String) {
let index = self let index = self
.accounts .accounts

View file

@ -88,6 +88,15 @@ fn main() {
App::new("setup") App::new("setup")
.about("Set up a new account with steamguard-cli") .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( .subcommand(
App::new("remove") App::new("remove")
.about("Remove the authenticator from an account.") .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; return;
} }