feat: Support export and import with different password #30

Merged
awiteb merged 5 commits from awiteb/fix-25 into master 2024-05-08 05:06:28 +02:00 AGit
3 changed files with 7 additions and 4 deletions
Showing only changes of commit 5d1398832c - Show all commits

View file

@ -38,7 +38,7 @@ pub struct Add {
impl LprsCommand for Add { impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> {
self.vault_info.password = utils::user_password(self.password)?; self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
vault_manager.add_vault(self.vault_info); vault_manager.add_vault(self.vault_info);
vault_manager.try_export() vault_manager.try_export()
} }

View file

@ -68,7 +68,7 @@ impl LprsCommand for Edit {
*vault = Vault::new( *vault = Vault::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()),
utils::user_password(self.password)? utils::user_password(self.password, "New vault password:")?
.as_ref() .as_ref()
.or(vault.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()),

View file

@ -52,14 +52,17 @@ pub fn local_project_file(filename: &str) -> LprsResult<PathBuf> {
/// ///
/// ## Errors /// ## Errors
/// - When failed to get the password from stdin /// - When failed to get the password from stdin
pub fn user_password(password: Option<Option<String>>) -> LprsResult<Option<String>> { pub fn user_password(
password: Option<Option<String>>,
prompt_message: &str,
) -> LprsResult<Option<String>> {
Ok(match password { Ok(match password {
None => None, None => None,
Some(Some(p)) => Some(p), Some(Some(p)) => Some(p),
Some(None) => { Some(None) => {
log::debug!("User didn't provide a password, prompting it"); log::debug!("User didn't provide a password, prompting it");
Some( Some(
Password::new("Vault password:") Password::new(prompt_message)
.without_confirmation() .without_confirmation()
.with_formatter(&|p| "*".repeat(p.chars().count())) .with_formatter(&|p| "*".repeat(p.chars().count()))
.with_display_mode(PasswordDisplayMode::Masked) .with_display_mode(PasswordDisplayMode::Masked)