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,31 +39,37 @@ 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<()> {
|
||||||
|
if !self.vault_info.is_empty() {
|
||||||
self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
|
self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
|
||||||
self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
|
self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
|
||||||
vault_manager.add_vault(self.vault_info);
|
vault_manager.add_vault(self.vault_info);
|
||||||
vault_manager.try_export()
|
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) {
|
||||||
|
if !self.force {
|
||||||
return Err(LprsError::Other(format!(
|
return Err(LprsError::Other(format!(
|
||||||
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,10 +104,12 @@ 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) {
|
||||||
|
if !self.force {
|
||||||
return Err(LprsError::Other(format!(
|
return Err(LprsError::Other(format!(
|
||||||
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
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
|
/// 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;
|
||||||
|
|
Loading…
Reference in a new issue