From 5d30b8b021d5e2172aa8bbaafaa31c10980c4107 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 24 Jul 2024 10:35:42 +0300 Subject: [PATCH 1/2] feat: Remove vault field if its value is empty string Signed-off-by: Awiteb --- src/cli/edit_command.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 6241bf4..6fd2ee2 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -82,23 +82,43 @@ impl LprsCommand for Edit { if let Some(new_name) = self.name { vault.name = new_name; } - if self.password.is_some() { - vault.password = utils::user_secret(self.password, "New vault password:", false)?; + if let Some(ref new_password) = self.password { + if new_password.as_deref().is_some_and(|s| s.is_empty()) { + vault.password = None; + } else { + vault.password = utils::user_secret(self.password, "New vault password:", false)?; + } } if let Some(totp_secret) = utils::user_secret(self.totp_secret, "TOTP Secret:", false)? { - cipher::base32_decode(&totp_secret).map_err(|_| { - LprsError::Base32("Invalid TOTP secret, must be valid base32 string".to_owned()) - })?; - vault.totp_secret = Some(totp_secret); + if totp_secret.is_empty() { + vault.totp_secret = None; + } else { + cipher::base32_decode(&totp_secret).map_err(|_| { + LprsError::Base32("Invalid TOTP secret, must be valid base32 string".to_owned()) + })?; + vault.totp_secret = Some(totp_secret); + } } if let Some(new_username) = self.username { - vault.username = Some(new_username); + if new_username.is_empty() { + vault.username = None; + } else { + vault.username = Some(new_username); + } } if let Some(new_service) = self.service { - vault.service = Some(new_service); + if new_service.is_empty() { + vault.service = None; + } else { + vault.service = Some(new_service); + } } if let Some(new_note) = self.note { - vault.note = Some(new_note); + if new_note.is_empty() { + vault.note = None; + } else { + vault.note = Some(new_note); + } } utils::apply_custom_fields(&mut vault.custom_fields, self.custom_fields); -- 2.45.2 From 3567865c2da0c994fbaa549dd4346640db3f4bf7 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 24 Jul 2024 10:36:17 +0300 Subject: [PATCH 2/2] docs: Update `edit` command docs Signed-off-by: Awiteb --- docs/commands/edit.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/commands/edit.md b/docs/commands/edit.md index c1ed248..5e11789 100644 --- a/docs/commands/edit.md +++ b/docs/commands/edit.md @@ -55,6 +55,10 @@ custom fields. For secrets like the password and TOTP secret, you can provide them as arguments or you will be prompted for them. +## Field removal +If you want to remove a field from the vault, you can provide an empty value for +it, e.g. `-o ""`. + ## Custom fields If you want to add a custom field to the vault, you can use the `-c, --custom` option, and provide the key-value pair. If you want to delete a custom field, -- 2.45.2