From cf5dca211197c0dee3563fc468b4f3d0dd3b7927 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Thu, 25 Apr 2024 01:06:33 +0300 Subject: [PATCH] feat: Make the password value optional in `edit` command This will prompt the user for the password in the stdin Related-to: awiteb/lprs#6 Issue-Tracker: https://git.4rs.nl/awiteb/lprs/issues --- src/cli/edit_command.rs | 63 +++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 7c05b8e..7c594f9 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -36,8 +36,8 @@ pub struct Edit { /// The new vault username username: Option, #[arg(short, long)] - /// The new password - password: Option, + /// The new password, if there is no value for it you will prompt it + password: Option>, #[arg(short, long)] /// The new vault service service: Option, @@ -50,32 +50,45 @@ impl RunCommand for Edit { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = self.index.get() as usize; - if let Some(vault) = vault_manager.vaults.get_mut(index - 1) { - if self.name.is_none() - && self.username.is_none() - && self.password.is_none() - && self.service.is_none() - && self.note.is_none() - { - Err(LprsError::Other( - "You must edit one option at least".to_owned(), - )) - } else { - *vault = Vault::::new( - self.name.as_ref().unwrap_or(&vault.name), - self.username.as_ref().or(vault.username.as_ref()), - self.password.as_ref().or(vault.password.as_ref()), - self.service.as_ref().or(vault.service.as_ref()), - self.note.as_ref().or(vault.note.as_ref()), - ); - vault_manager.try_export() - } - } else { - Err(LprsError::InvalidVaultIndex(format!( + let Some(vault) = vault_manager.vaults.get_mut(index - 1) else { + return Err(LprsError::InvalidVaultIndex(format!( "The index `{}` is greater than the vaults count {}", self.index, vault_manager.vaults.len() - ))) + ))); + }; + + if self.name.is_none() + && self.username.is_none() + && self.password.is_none() + && self.service.is_none() + && self.note.is_none() + { + return Err(LprsError::Other( + "You must edit one option at least".to_owned(), + )); } + + // Get the password from stdin or from its value if provided + let password = match self.password { + Some(Some(password)) => Some(password), + Some(None) => Some( + inquire::Password::new("New vault password:") + .without_confirmation() + .with_formatter(&|p| "*".repeat(p.chars().count())) + .with_display_mode(inquire::PasswordDisplayMode::Masked) + .prompt()?, + ), + None => None, + }; + + *vault = Vault::::new( + self.name.as_ref().unwrap_or(&vault.name), + self.username.as_ref().or(vault.username.as_ref()), + password.as_ref().or(vault.password.as_ref()), + self.service.as_ref().or(vault.service.as_ref()), + self.note.as_ref().or(vault.note.as_ref()), + ); + vault_manager.try_export() } }