From 73281ef069505687dd670e591c5b5a79ed987324 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 19 Jun 2022 11:43:37 -0400 Subject: [PATCH] move import subcommand impl --- src/accountmanager.rs | 6 +++--- src/main.rs | 36 +++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/accountmanager.rs b/src/accountmanager.rs index 2f82556..73dfb60 100644 --- a/src/accountmanager.rs +++ b/src/accountmanager.rs @@ -180,8 +180,8 @@ impl Manifest { .insert(account.account_name.clone(), Arc::new(Mutex::new(account))); } - pub fn import_account(&mut self, import_path: String) -> anyhow::Result<()> { - let path = Path::new(&import_path); + 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); @@ -576,7 +576,7 @@ mod tests { let mut loaded_manifest = Manifest::new(manifest_path.as_path()); assert!(matches!( loaded_manifest.import_account( - tmp_dir + &tmp_dir .path() .join("asdf1234.maFile") .into_os_string() diff --git a/src/main.rs b/src/main.rs index 78e96a1..d5a632f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,7 +229,9 @@ fn run() -> anyhow::Result<()> { Some(cli::Subcommands::Setup(args)) => { return do_subcmd_setup(args, &mut manifest); }, - Some(cli::Subcommands::Import(args)) => {todo!()}, + Some(cli::Subcommands::Import(args)) => { + return do_subcmd_import(args, &mut manifest); + }, Some(cli::Subcommands::Encrypt(args)) => { return do_subcmd_encrypt(args, &mut manifest); }, @@ -239,22 +241,6 @@ fn run() -> anyhow::Result<()> { _ => {}, } - 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) => { - bail!("Failed to import account: {} {}", file_path, err); - } - } - } - - manifest.save()?; - return Ok(()); - } - let mut selected_accounts: Vec>>; loop { match get_selected_accounts(&matches, &mut manifest) { @@ -682,6 +668,22 @@ fn do_subcmd_setup(args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest return Ok(()); } +fn do_subcmd_import(args: cli::ArgsImport, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { + for file_path in args.files { + match manifest.import_account(&file_path) { + Ok(_) => { + info!("Imported account: {}", &file_path); + } + Err(err) => { + bail!("Failed to import account: {} {}", &file_path, err); + } + } + } + + manifest.save()?; + return Ok(()); +} + fn do_subcmd_encrypt(args: cli::ArgsEncrypt, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { if !manifest.has_passkey() { let mut passkey;