From 4028607cb5ba0baaf6671d65fa46d433aba554a7 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sat, 17 Aug 2024 14:52:14 +0000 Subject: [PATCH] feat: Support entering custom keys value via STDIN This for adding new key, and editing keys (Change the key value) Fixes: https://git.4rs.nl/awiteb/lprs/issues/56 Signed-off-by: Awiteb --- src/cli/add_command.rs | 20 ++++++++++++-------- src/cli/edit_command.rs | 24 ++++++++++++++++-------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 1b02d0e..1fdaa8f 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -39,9 +39,11 @@ pub struct Add { #[allow(clippy::option_option)] totp_secret: Option>, /// Add a custom field to the vault - #[arg(name = "KEY=VALUE", short = 'c', long = "custom")] + /// + /// If there is no value, you will enter it through a prompt + #[arg(name = "KEY(=VALUE)?", short = 'c', long = "custom")] #[arg(value_parser = clap_parsers::kv_parser)] - custom_fields: Vec<(String, String)>, + custom_fields: Vec<(String, Option)>, /// Force add, will not return error if there is a problem with the args. /// /// For example, duplication in the custom fields and try to adding empty @@ -73,7 +75,9 @@ impl LprsCommand for Add { self.vault_info.name = self.vault_info.name.trim().to_string(); self.vault_info.password = utils::user_secret(self.password, "Vault password:", false)?; - self.vault_info.custom_fields = self.custom_fields.into_iter().collect(); + self.vault_info.custom_fields = utils::prompt_custom(self.custom_fields)? + .into_iter() + .collect(); vault_manager.add_vault(self.vault_info); vault_manager.try_export()?; } @@ -95,23 +99,23 @@ impl LprsCommand for Add { if self .password .as_ref() - .is_some_and(|p| p.as_ref().is_some_and(|p| p.is_empty())) + .is_some_and(|p| p.as_ref().is_some_and(String::is_empty)) || self.vault_info.name.is_empty() || self .vault_info .username .as_ref() - .is_some_and(|u| u.is_empty()) + .is_some_and(String::is_empty) || self .vault_info .service .as_ref() - .is_some_and(|s| s.is_empty()) - || self.vault_info.note.as_ref().is_some_and(|n| n.is_empty()) + .is_some_and(String::is_empty) + || self.vault_info.note.as_ref().is_some_and(String::is_empty) || self .custom_fields .iter() - .any(|(k, v)| k.is_empty() || v.is_empty()) + .any(|(k, v)| k.is_empty() || v.as_ref().is_some_and(String::is_empty)) { return Err(LprsError::EmptyValue); } diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 6fd2ee2..23ad011 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -36,28 +36,33 @@ pub struct Edit { /// The new vault name name: Option, #[arg(short, long)] - /// The new vault username + /// The new vault username, make it empty string to delete it username: Option, #[arg(short, long)] - /// The new password, if there is no value for it you will prompt it + /// The new password, make it empty string to delete it + /// + /// If there is no value for it you will prompt it #[allow(clippy::option_option)] password: Option>, #[arg(short, long)] - /// The new vault service + /// The new vault service, make it empty string to delete it service: Option, #[arg(short = 'o', long)] /// The new vault note note: Option, - /// The TOTP secret, if there is no value you will prompt it + /// The TOTP secret, make it empty string to delete it + /// + /// If there is no value you will prompt it #[arg(short, long)] #[allow(clippy::option_option)] totp_secret: Option>, - /// The custom field, make its value empty to delete it + /// The custom field, make it empty string to delete it /// - /// If the custom field not exist will created it, if it's will update it + /// If the custom field not exist will created it, if it's will update it, + /// if there is no value, you will enter it through a prompt (e.g `-c key`) #[arg(name = "KEY=VALUE", short = 'c', long = "custom")] #[arg(value_parser = clap_parsers::kv_parser)] - custom_fields: Vec<(String, String)>, + custom_fields: Vec<(String, Option)>, /// Force edit, will not return error if there is a problem with the args. /// /// For example, duplication in the custom fields and try to editing nothing @@ -120,7 +125,10 @@ impl LprsCommand for Edit { vault.note = Some(new_note); } } - utils::apply_custom_fields(&mut vault.custom_fields, self.custom_fields); + utils::apply_custom_fields( + &mut vault.custom_fields, + utils::prompt_custom(self.custom_fields)?, + ); vault_manager.try_export() }