feat: Force flag for edit
and add
commands (#42)
Reviewed-on: #42 Co-authored-by: Awiteb <a@4rs.nl> Co-committed-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
8371e144a1
commit
add008416b
3 changed files with 40 additions and 17 deletions
|
@ -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(())
|
||||
|
|
|
@ -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(())
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue