feat: Ability to enter password via stdin add
&edit
#15
1 changed files with 38 additions and 25 deletions
|
@ -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 {
|
||||||
if self.name.is_none()
|
return Err(LprsError::InvalidVaultIndex(format!(
|
||||||
&& 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::<Plain>::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!(
|
|
||||||
"The index `{}` is greater than the vaults count {}",
|
"The index `{}` is greater than the vaults count {}",
|
||||||
self.index,
|
self.index,
|
||||||
vault_manager.vaults.len()
|
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::<Plain>::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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue