From c0bba24cdf7143a1fc7a3879a15050b015601814 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Thu, 25 Apr 2024 00:04:15 +0300 Subject: [PATCH] feat: Make the password value optional in `add` 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/add_command.rs | 24 +++++++++++++++++++++--- src/vault/mod.rs | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index e5fadd1..ce3c8df 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -26,18 +26,36 @@ use crate::{ pub struct Add { #[command(flatten)] vault_info: Vault, + /// The password, if there is no value for it you will prompt it + #[arg(short, long)] + password: Option>, } impl RunCommand for Add { - fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { if self.vault_info.username.is_none() - && self.vault_info.password.is_none() + && self.password.is_none() && self.vault_info.service.is_none() && self.vault_info.note.is_none() { return Err(LprsError::Other("You can't add empty vault".to_owned())); } - vault_manager.add_vault(self.vault_info.clone()); + + match self.password { + Some(Some(password)) => self.vault_info.password = Some(password), + Some(None) => { + self.vault_info.password = Some( + inquire::Password::new("Vault password:") + .without_confirmation() + .with_formatter(&|p| "*".repeat(p.chars().count())) + .with_display_mode(inquire::PasswordDisplayMode::Masked) + .prompt()?, + ); + } + None => {} + }; + + vault_manager.add_vault(self.vault_info); vault_manager.try_export() } } diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 5f47c53..55b4917 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -60,7 +60,7 @@ where #[arg(short, long)] pub username: Option, /// The password - #[arg(short, long)] + #[arg(skip)] pub password: Option, /// The service name. e.g the website url #[arg(short, long)]