From add008416b37c3f8e4def891355dcbccc6786a58 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Fri, 10 May 2024 13:56:28 +0200 Subject: [PATCH] feat: Force flag for `edit` and `add` commands (#42) Reviewed-on: https://git.4rs.nl/awiteb/lprs/pulls/42 Co-authored-by: Awiteb Co-committed-by: Awiteb --- src/cli/add_command.rs | 32 +++++++++++++++++++------------- src/cli/edit_command.rs | 16 ++++++++++++---- src/vault/mod.rs | 9 +++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 5c211c2..2c3a9b3 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -39,30 +39,36 @@ pub struct Add { #[arg(name = "KEY=VALUE", short = 'c', long = "custom")] #[arg(value_parser = clap_parsers::kv_parser)] custom_fields: Vec<(String, String)>, + /// 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 + /// vault + #[arg(short, long)] + force: bool, } impl LprsCommand for Add { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { - self.vault_info.password = utils::user_password(self.password, "Vault password:")?; - self.vault_info.custom_fields = self.custom_fields.into_iter().collect(); - vault_manager.add_vault(self.vault_info); - vault_manager.try_export() + if !self.vault_info.is_empty() { + self.vault_info.password = utils::user_password(self.password, "Vault password:")?; + self.vault_info.custom_fields = self.custom_fields.into_iter().collect(); + vault_manager.add_vault(self.vault_info); + vault_manager.try_export()?; + } + Ok(()) } fn validate_args(&self) -> LprsResult<()> { - if self.vault_info.username.is_none() - && self.password.is_none() - && self.vault_info.service.is_none() - && self.vault_info.note.is_none() - && self.custom_fields.is_empty() - { + if !self.force && self.vault_info.is_empty() { return Err(LprsError::Other("You can't add empty vault".to_owned())); } if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) { - return Err(LprsError::Other(format!( - "Duplication error: The custom key `{duplicated_key}` is duplicate" - ))); + if !self.force { + return Err(LprsError::Other(format!( + "Duplication error: The custom key `{duplicated_key}` is duplicate" + ))); + } } Ok(()) diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index d264576..98ec47b 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -49,6 +49,11 @@ pub struct Edit { #[arg(name = "KEY=VALUE", short = 'c', long = "custom")] #[arg(value_parser = clap_parsers::kv_parser)] pub custom_fields: Vec<(String, String)>, + /// 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 + #[arg(short, long)] + force: bool, } impl LprsCommand for Edit { @@ -86,7 +91,8 @@ impl LprsCommand for Edit { } fn validate_args(&self) -> LprsResult<()> { - if self.name.is_none() + if !self.force + && self.name.is_none() && self.username.is_none() && self.password.is_none() && self.service.is_none() @@ -98,9 +104,11 @@ impl LprsCommand for Edit { )); } if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) { - return Err(LprsError::Other(format!( - "Duplication error: The custom key `{duplicated_key}` is duplicate" - ))); + if !self.force { + return Err(LprsError::Other(format!( + "Duplication error: The custom key `{duplicated_key}` is duplicate" + ))); + } } Ok(()) diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 931b317..7c0c022 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -94,6 +94,15 @@ impl Vault { } } + /// Returns true if the vault is empty + pub fn is_empty(&self) -> bool { + self.username.is_none() + && self.password.is_none() + && self.service.is_none() + && self.note.is_none() + && self.custom_fields.is_empty() + } + /// Return the name of the vault with the service if there pub fn list_name(&self) -> String { use fmt::Write;