From 621ec6a07aca0f588d868e1973ae5a9f1fd41831 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 7 May 2024 09:49:23 +0300 Subject: [PATCH] chore: Create `user_password` function and use it --- src/cli/add_command.rs | 22 ++-------------------- src/cli/edit_command.rs | 22 ++++------------------ src/utils.rs | 28 +++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 02200b5..29d986a 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -15,9 +15,9 @@ // along with this program. If not, see . use clap::Args; -use inquire::{Password, PasswordDisplayMode}; use crate::{ + utils, vault::{Vault, Vaults}, LprsCommand, LprsError, @@ -32,31 +32,13 @@ pub struct Add { vault_info: Vault, /// The password, if there is no value for it you will prompt it #[arg(short, long)] - // FIXME: I think replacing `Option>` with custom type will be better #[allow(clippy::option_option)] password: Option>, } impl LprsCommand for Add { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { - match self.password { - Some(Some(password)) => { - log::debug!("User provided a password"); - self.vault_info.password = Some(password); - } - Some(None) => { - log::debug!("User didn't provide a password, prompting it"); - self.vault_info.password = Some( - Password::new("Vault password:") - .without_confirmation() - .with_formatter(&|p| "*".repeat(p.chars().count())) - .with_display_mode(PasswordDisplayMode::Masked) - .prompt()?, - ); - } - None => {} - }; - + self.vault_info.password = utils::user_password(self.password)?; vault_manager.add_vault(self.vault_info); vault_manager.try_export() } diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 33e0d6b..aefae0c 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -17,9 +17,9 @@ use std::num::NonZeroU64; use clap::Args; -use inquire::{Password, PasswordDisplayMode}; use crate::{ + utils, vault::{Vault, Vaults}, LprsCommand, LprsError, @@ -41,7 +41,6 @@ pub struct Edit { username: Option, #[arg(short, long)] /// The new password, if there is no value for it you will prompt it - // FIXME: I think replacing `Option>` with custom type will be better #[allow(clippy::option_option)] password: Option>, #[arg(short, long)] @@ -65,26 +64,13 @@ impl LprsCommand for Edit { ))); }; - // Get the password from stdin or from its value if provided - let password = match self.password { - Some(Some(password)) => Some(password), - Some(None) => { - Some( - Password::new("New vault password:") - .without_confirmation() - .with_formatter(&|p| "*".repeat(p.chars().count())) - .with_display_mode(PasswordDisplayMode::Masked) - .prompt()?, - ) - } - None => None, - }; - log::info!("Applying the new values to the vault"); *vault = Vault::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()), + utils::user_password(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()), ); diff --git a/src/utils.rs b/src/utils.rs index 860d797..a9210fd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,7 +16,7 @@ use std::{fs, path::PathBuf}; -use inquire::{validator::Validation, PasswordDisplayMode}; +use inquire::{validator::Validation, Password, PasswordDisplayMode}; use passwords::{analyzer, scorer}; #[cfg(feature = "update-notify")] use reqwest::blocking::Client as BlockingClient; @@ -43,6 +43,32 @@ pub fn local_project_file(filename: &str) -> LprsResult { Ok(local_dir.join(filename)) } +/// Returns the user password if any +/// +/// - If the `password` is `None` will return `None` +/// - If the `password` is `Some(None)` will ask the user for a password in the +/// stdin and return it +/// - If the `password` is `Some(Some(password))` will return `Some(password)` +/// +/// ## Errors +/// - When failed to get the password from stdin +pub fn user_password(password: Option>) -> LprsResult> { + Ok(match password { + None => None, + Some(Some(p)) => Some(p), + Some(None) => { + log::debug!("User didn't provide a password, prompting it"); + Some( + Password::new("Vault password:") + .without_confirmation() + .with_formatter(&|p| "*".repeat(p.chars().count())) + .with_display_mode(PasswordDisplayMode::Masked) + .prompt()?, + ) + } + }) +} + /// Returns the default vaults json file /// /// ## Errors