feat: Ability to enter password via stdin add&edit #15

Merged
awiteb merged 4 commits from awiteb/fix-6 into master 2024-04-25 00:27:56 +02:00 AGit
Showing only changes of commit cf5dca2111 - Show all commits

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()
)))
}
}
} }