move encrypt and decrypt subcommand impls to functions

This commit is contained in:
Carson McManus 2022-06-19 11:40:20 -04:00
parent 138f36f562
commit 37539f7679

View file

@ -230,14 +230,16 @@ fn run() -> anyhow::Result<()> {
return do_subcmd_setup(args, &mut manifest); return do_subcmd_setup(args, &mut manifest);
}, },
Some(cli::Subcommands::Import(args)) => {todo!()}, Some(cli::Subcommands::Import(args)) => {todo!()},
Some(cli::Subcommands::Encrypt(args)) => {todo!()}, Some(cli::Subcommands::Encrypt(args)) => {
Some(cli::Subcommands::Decrypt(args)) => {todo!()}, return do_subcmd_encrypt(args, &mut manifest);
},
Some(cli::Subcommands::Decrypt(args)) => {
return do_subcmd_decrypt(args, &mut manifest);
},
_ => {}, _ => {},
} }
if matches.is_present("setup") { if let Some(import_matches) = matches.subcommand_matches("import") {
} else if let Some(import_matches) = matches.subcommand_matches("import") {
for file_path in import_matches.values_of("files").unwrap() { for file_path in import_matches.values_of("files").unwrap() {
match manifest.import_account(file_path.into()) { match manifest.import_account(file_path.into()) {
Ok(_) => { Ok(_) => {
@ -249,33 +251,6 @@ fn run() -> anyhow::Result<()> {
} }
} }
manifest.save()?;
return Ok(());
} else if matches.is_present("encrypt") {
if !manifest.has_passkey() {
loop {
passkey = rpassword::prompt_password_stdout("Enter encryption passkey: ").ok();
let passkey_confirm =
rpassword::prompt_password_stdout("Confirm encryption passkey: ").ok();
if passkey == passkey_confirm {
break;
}
error!("Passkeys do not match, try again.");
}
manifest.submit_passkey(passkey);
}
manifest.load_accounts()?;
for entry in &mut manifest.entries {
entry.encryption = Some(accountmanager::EntryEncryptionParams::generate());
}
manifest.save()?;
return Ok(());
} else if matches.is_present("decrypt") {
manifest.load_accounts()?;
for entry in &mut manifest.entries {
entry.encryption = None;
}
manifest.submit_passkey(None);
manifest.save()?; manifest.save()?;
return Ok(()); return Ok(());
} }
@ -706,3 +681,35 @@ fn do_subcmd_setup(args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest
return Ok(()); return Ok(());
} }
fn do_subcmd_encrypt(args: cli::ArgsEncrypt, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> {
if !manifest.has_passkey() {
let mut passkey;
loop {
passkey = rpassword::prompt_password_stdout("Enter encryption passkey: ").ok();
let passkey_confirm =
rpassword::prompt_password_stdout("Confirm encryption passkey: ").ok();
if passkey == passkey_confirm {
break;
}
error!("Passkeys do not match, try again.");
}
manifest.submit_passkey(passkey);
}
manifest.load_accounts()?;
for entry in &mut manifest.entries {
entry.encryption = Some(accountmanager::EntryEncryptionParams::generate());
}
manifest.save()?;
return Ok(());
}
fn do_subcmd_decrypt(args: cli::ArgsDecrypt, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> {
manifest.load_accounts()?;
for entry in &mut manifest.entries {
entry.encryption = None;
}
manifest.submit_passkey(None);
manifest.save()?;
return Ok(());
}