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
This commit is contained in:
Awiteb 2024-04-25 00:04:15 +03:00
parent 6666a88cc5
commit c0bba24cdf
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 22 additions and 4 deletions

View file

@ -26,18 +26,36 @@ use crate::{
pub struct Add {
#[command(flatten)]
vault_info: Vault<Plain>,
/// The password, if there is no value for it you will prompt it
#[arg(short, long)]
password: Option<Option<String>>,
}
impl RunCommand for Add {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
fn run(mut self, mut vault_manager: Vaults<Plain>) -> 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()
}
}

View file

@ -60,7 +60,7 @@ where
#[arg(short, long)]
pub username: Option<String>,
/// The password
#[arg(short, long)]
#[arg(skip)]
pub password: Option<String>,
/// The service name. e.g the website url
#[arg(short, long)]