feat: Force flag for edit and add commands
All checks were successful
Rust CI / Rust CI (pull_request) Successful in 1m55s

This commit is contained in:
Awiteb 2024-05-10 14:19:19 +03:00
parent 8371e144a1
commit 15227b846c
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 40 additions and 17 deletions

View file

@ -39,30 +39,36 @@ pub struct Add {
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")] #[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
#[arg(value_parser = clap_parsers::kv_parser)] #[arg(value_parser = clap_parsers::kv_parser)]
custom_fields: Vec<(String, String)>, 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 { impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> {
self.vault_info.password = utils::user_password(self.password, "Vault password:")?; if !self.vault_info.is_empty() {
self.vault_info.custom_fields = self.custom_fields.into_iter().collect(); self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
vault_manager.add_vault(self.vault_info); self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
vault_manager.try_export() vault_manager.add_vault(self.vault_info);
vault_manager.try_export()?;
}
Ok(())
} }
fn validate_args(&self) -> LprsResult<()> { fn validate_args(&self) -> LprsResult<()> {
if self.vault_info.username.is_none() if !self.force && self.vault_info.is_empty() {
&& self.password.is_none()
&& self.vault_info.service.is_none()
&& self.vault_info.note.is_none()
&& self.custom_fields.is_empty()
{
return Err(LprsError::Other("You can't add empty vault".to_owned())); return Err(LprsError::Other("You can't add empty vault".to_owned()));
} }
if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) { if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) {
return Err(LprsError::Other(format!( if !self.force {
"Duplication error: The custom key `{duplicated_key}` is duplicate" return Err(LprsError::Other(format!(
))); "Duplication error: The custom key `{duplicated_key}` is duplicate"
)));
}
} }
Ok(()) Ok(())

View file

@ -49,6 +49,11 @@ pub struct Edit {
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")] #[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
#[arg(value_parser = clap_parsers::kv_parser)] #[arg(value_parser = clap_parsers::kv_parser)]
pub custom_fields: Vec<(String, String)>, 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 { impl LprsCommand for Edit {
@ -86,7 +91,8 @@ impl LprsCommand for Edit {
} }
fn validate_args(&self) -> LprsResult<()> { fn validate_args(&self) -> LprsResult<()> {
if self.name.is_none() if !self.force
&& self.name.is_none()
&& self.username.is_none() && self.username.is_none()
&& self.password.is_none() && self.password.is_none()
&& self.service.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) { if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) {
return Err(LprsError::Other(format!( if !self.force {
"Duplication error: The custom key `{duplicated_key}` is duplicate" return Err(LprsError::Other(format!(
))); "Duplication error: The custom key `{duplicated_key}` is duplicate"
)));
}
} }
Ok(()) Ok(())

View file

@ -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 /// Return the name of the vault with the service if there
pub fn list_name(&self) -> String { pub fn list_name(&self) -> String {
use fmt::Write; use fmt::Write;