feat: Make the password value optional in edit command
Some checks failed
Check PR title / check_pr_title (pull_request) Successful in 3s
Rust CI / Rust CI (pull_request) Failing after 1m18s

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 01:06:33 +03:00
parent c0bba24cdf
commit cf5dca2111
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -36,8 +36,8 @@ pub struct Edit {
/// The new vault username /// The new vault username
username: Option<String>, username: Option<String>,
#[arg(short, long)] #[arg(short, long)]
/// The new password /// The new password, if there is no value for it you will prompt it
password: Option<String>, password: Option<Option<String>>,
#[arg(short, long)] #[arg(short, long)]
/// The new vault service /// The new vault service
service: Option<String>, service: Option<String>,
@ -50,32 +50,45 @@ impl RunCommand for Edit {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> { fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
let index = self.index.get() as usize; let index = self.index.get() as usize;
if let Some(vault) = vault_manager.vaults.get_mut(index - 1) { 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() if 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()
&& self.note.is_none() && self.note.is_none()
{ {
Err(LprsError::Other( return Err(LprsError::Other(
"You must edit one option at least".to_owned(), "You must edit one option at least".to_owned(),
)) ));
} else { }
// 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::<Plain>::new( *vault = Vault::<Plain>::new(
self.name.as_ref().unwrap_or(&vault.name), self.name.as_ref().unwrap_or(&vault.name),
self.username.as_ref().or(vault.username.as_ref()), self.username.as_ref().or(vault.username.as_ref()),
self.password.as_ref().or(vault.password.as_ref()), password.as_ref().or(vault.password.as_ref()),
self.service.as_ref().or(vault.service.as_ref()), self.service.as_ref().or(vault.service.as_ref()),
self.note.as_ref().or(vault.note.as_ref()), self.note.as_ref().or(vault.note.as_ref()),
); );
vault_manager.try_export() vault_manager.try_export()
} }
} else {
Err(LprsError::InvalidVaultIndex(format!(
"The index `{}` is greater than the vaults count {}",
self.index,
vault_manager.vaults.len()
)))
}
}
} }