From 21429104a360347edf334bb33db775454f537f6d Mon Sep 17 00:00:00 2001 From: Awiteb Date: Thu, 25 Apr 2024 07:42:06 +0300 Subject: [PATCH 1/3] chore: Rename `RunCommand` trait to `LprsCommand` --- src/cli/add_command.rs | 4 ++-- src/cli/clean_command.rs | 4 ++-- src/cli/edit_command.rs | 4 ++-- src/cli/export_command.rs | 4 ++-- src/cli/gen_command.rs | 4 ++-- src/cli/import_command.rs | 4 ++-- src/cli/list_command.rs | 4 ++-- src/cli/mod.rs | 2 +- src/cli/remove_command.rs | 4 ++-- src/macros.rs | 8 ++++---- src/traits.rs | 4 ++-- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index ce3c8df..1d6d86f 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -18,7 +18,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vault, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -31,7 +31,7 @@ pub struct Add { password: Option>, } -impl RunCommand for Add { +impl LprsCommand for Add { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { if self.vault_info.username.is_none() && self.password.is_none() diff --git a/src/cli/clean_command.rs b/src/cli/clean_command.rs index 11ae7ec..989bf1d 100644 --- a/src/cli/clean_command.rs +++ b/src/cli/clean_command.rs @@ -20,14 +20,14 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] pub struct Clean {} -impl RunCommand for Clean { +impl LprsCommand for Clean { fn run(self, vault_manager: Vaults) -> LprsResult<()> { fs::write(vault_manager.vaults_file, "[]").map_err(LprsError::Io) } diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 7c594f9..8e1052f 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vault, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -46,7 +46,7 @@ pub struct Edit { note: Option, } -impl RunCommand for Edit { +impl LprsCommand for Edit { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = self.index.get() as usize; diff --git a/src/cli/export_command.rs b/src/cli/export_command.rs index 34b7abf..fd46e81 100644 --- a/src/cli/export_command.rs +++ b/src/cli/export_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, BitWardenPasswords, Format, Vault, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -33,7 +33,7 @@ pub struct Export { format: Format, } -impl RunCommand for Export { +impl LprsCommand for Export { fn run(self, vault_manager: Vaults) -> LprsResult<()> { if self .path diff --git a/src/cli/gen_command.rs b/src/cli/gen_command.rs index f35f92f..382d94a 100644 --- a/src/cli/gen_command.rs +++ b/src/cli/gen_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -44,7 +44,7 @@ pub struct Gen { symbols: bool, } -impl RunCommand for Gen { +impl LprsCommand for Gen { fn run(self, _vault_manager: Vaults) -> LprsResult<()> { if self.uppercase || self.lowercase || self.numbers || self.symbols { println!( diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index f9de214..4bdb5f7 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, BitWardenPasswords, Format, Vault, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -34,7 +34,7 @@ pub struct Import { format: Format, } -impl RunCommand for Import { +impl LprsCommand for Import { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { if self.path.exists() { if self diff --git a/src/cli/list_command.rs b/src/cli/list_command.rs index 16cfc63..d0f0cc1 100644 --- a/src/cli/list_command.rs +++ b/src/cli/list_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -49,7 +49,7 @@ pub struct List { regex: bool, } -impl RunCommand for List { +impl LprsCommand for List { fn run(self, vault_manager: Vaults) -> LprsResult<()> { if vault_manager.vaults.is_empty() { return Err(LprsError::Other( diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 65298e2..933f75b 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -18,7 +18,7 @@ use std::path::PathBuf; use clap::Parser; -use crate::{utils, vault::Vaults, LprsResult, RunCommand}; +use crate::{utils, vault::Vaults, LprsCommand, LprsResult}; pub mod add_command; pub mod clean_command; diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs index 36e8ebb..af0ef12 100644 --- a/src/cli/remove_command.rs +++ b/src/cli/remove_command.rs @@ -20,7 +20,7 @@ use clap::Args; use crate::{ vault::{vault_state::*, Vaults}, - LprsError, LprsResult, RunCommand, + LprsCommand, LprsError, LprsResult, }; #[derive(Debug, Args)] @@ -34,7 +34,7 @@ pub struct Remove { force: bool, } -impl RunCommand for Remove { +impl LprsCommand for Remove { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = (self.index.get() - 1) as usize; if index > vault_manager.vaults.len() { diff --git a/src/macros.rs b/src/macros.rs index a753c97..aab1c8c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -14,10 +14,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -/// Creates commands macro, to create the `Commands` enum and impl `RunCommand` to it. +/// Creates commands macro, to create the `Commands` enum and impl `LprsCommand` to it. /// /// ### Notes: -/// The `$command` must impl `RunCommand` trait +/// The `$command` must impl `LprsCommand` trait /// /// ### Example: /// ```rust @@ -37,7 +37,7 @@ /// Some(SomeArgs), /// } /// -/// impl crate::RunCommand for TestCommands { +/// impl crate::LprsCommand for TestCommands { /// fn run( /// &self, /// vault_manager: crate::vault::Vaults, @@ -62,7 +62,7 @@ macro_rules! create_commands { } #[automatically_derived] - impl $crate::RunCommand for $enum_name{ + impl $crate::LprsCommand for $enum_name{ fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> { match self { $( diff --git a/src/traits.rs b/src/traits.rs index cb0882c..c48b693 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -19,7 +19,7 @@ use crate::{ LprsResult, }; -/// Trait to run the command -pub trait RunCommand { +/// Trait to work with the commands +pub trait LprsCommand { fn run(self, vault_manager: Vaults) -> LprsResult<()>; } -- 2.45.2 From a6870b2dcabc73427a56334b5a0ea45930f42f26 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Thu, 25 Apr 2024 08:29:54 +0300 Subject: [PATCH 2/3] chore: Ctrate `LprsCommand::validate_args` function This function will validate the args of the command before ask the user for the master password --- src/cli/mod.rs | 2 ++ src/macros.rs | 8 ++++++++ src/traits.rs | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 933f75b..a5c2424 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -63,6 +63,8 @@ impl Cli { }; log::debug!("Getting the vaults file: {}", vaults_file.to_string_lossy()); + self.command.validate_args()?; + let vault_manager = if matches!(self.command, Commands::Clean(..) | Commands::Gen(..)) { // Returns empty vault manager for those commands don't need it Vaults { diff --git a/src/macros.rs b/src/macros.rs index aab1c8c..69f2777 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -70,6 +70,14 @@ macro_rules! create_commands { )+ } } + + fn validate_args(&self) -> LprsResult<()> { + match self { + $( + Self::$varint(command) => command.validate_args(), + )+ + } + } } }; } diff --git a/src/traits.rs b/src/traits.rs index c48b693..708829a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -21,5 +21,11 @@ use crate::{ /// Trait to work with the commands pub trait LprsCommand { + /// Run the command, should do all the logic, even the export fn run(self, vault_manager: Vaults) -> LprsResult<()>; + + /// Validate the gaiven args from the user. + fn validate_args(&self) -> LprsResult<()> { + Ok(()) + } } -- 2.45.2 From 75bf74d51209b519ed6ba68d7b85ba653e993fa5 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Thu, 25 Apr 2024 08:31:33 +0300 Subject: [PATCH 3/3] feat: Validate args before ask for the master password Fixes: https://git.4rs.nl/awiteb/lprs/issues/13 --- src/cli/add_command.rs | 19 ++++++++------ src/cli/edit_command.rs | 25 ++++++++++-------- src/cli/export_command.rs | 22 +++++++++------- src/cli/gen_command.rs | 37 ++++++++++++++------------ src/cli/import_command.rs | 55 ++++++++++++++++++++------------------- 5 files changed, 85 insertions(+), 73 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 1d6d86f..dc7f5ef 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -33,14 +33,6 @@ pub struct Add { impl LprsCommand for Add { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { - if self.vault_info.username.is_none() - && self.password.is_none() - && self.vault_info.service.is_none() - && self.vault_info.note.is_none() - { - return Err(LprsError::Other("You can't add empty vault".to_owned())); - } - match self.password { Some(Some(password)) => self.vault_info.password = Some(password), Some(None) => { @@ -58,4 +50,15 @@ impl LprsCommand for Add { vault_manager.add_vault(self.vault_info); vault_manager.try_export() } + + fn validate_args(&self) -> LprsResult<()> { + if self.vault_info.username.is_none() + && self.password.is_none() + && self.vault_info.service.is_none() + && self.vault_info.note.is_none() + { + return Err(LprsError::Other("You can't add empty vault".to_owned())); + } + Ok(()) + } } diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 8e1052f..9fe9b17 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -58,17 +58,6 @@ impl LprsCommand for Edit { ))); }; - 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), @@ -91,4 +80,18 @@ impl LprsCommand for Edit { ); vault_manager.try_export() } + + fn validate_args(&self) -> LprsResult<()> { + 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(), + )); + } + Ok(()) + } } diff --git a/src/cli/export_command.rs b/src/cli/export_command.rs index fd46e81..56f18f5 100644 --- a/src/cli/export_command.rs +++ b/src/cli/export_command.rs @@ -35,22 +35,24 @@ pub struct Export { impl LprsCommand for Export { fn run(self, vault_manager: Vaults) -> LprsResult<()> { + let exported_data = match self.format { + Format::Lprs => { + serde_json::to_string::>>(&vault_manager.encrypt_vaults()?) + } + Format::BitWarden => serde_json::to_string(&BitWardenPasswords::from(vault_manager)), + }?; + + fs::write(&self.path, exported_data).map_err(LprsError::from) + } + + fn validate_args(&self) -> LprsResult<()> { if self .path .extension() .is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json")) { if !self.path.exists() { - let exported_data = match self.format { - Format::Lprs => serde_json::to_string::>>( - &vault_manager.encrypt_vaults()?, - ), - Format::BitWarden => { - serde_json::to_string(&BitWardenPasswords::from(vault_manager)) - } - }?; - - fs::write(&self.path, exported_data).map_err(LprsError::from) + Ok(()) } else { Err(LprsError::Io(IoError::new( IoErrorKind::AlreadyExists, diff --git a/src/cli/gen_command.rs b/src/cli/gen_command.rs index 382d94a..92dfae3 100644 --- a/src/cli/gen_command.rs +++ b/src/cli/gen_command.rs @@ -46,24 +46,27 @@ pub struct Gen { impl LprsCommand for Gen { fn run(self, _vault_manager: Vaults) -> LprsResult<()> { - if self.uppercase || self.lowercase || self.numbers || self.symbols { - println!( - "{}", - passwords::PasswordGenerator::new() - .length(self.length.get() as usize) - .uppercase_letters(self.uppercase) - .lowercase_letters(self.lowercase) - .numbers(self.numbers) - .symbols(self.symbols) - .strict(true) - .generate_one() - .expect("The length cannot be zero") - ); - Ok(()) - } else { - Err(LprsError::Other( + println!( + "{}", + passwords::PasswordGenerator::new() + .length(self.length.get() as usize) + .uppercase_letters(self.uppercase) + .lowercase_letters(self.lowercase) + .numbers(self.numbers) + .symbols(self.symbols) + .strict(true) + .generate_one() + .expect("The length cannot be zero") + ); + Ok(()) + } + + fn validate_args(&self) -> LprsResult<()> { + if !(self.uppercase || self.lowercase || self.numbers || self.symbols) { + return Err(LprsError::Other( "You need to enable at least one kind of characters".to_owned(), - )) + )); } + Ok(()) } } diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index 4bdb5f7..6c46762 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -36,39 +36,40 @@ pub struct Import { impl LprsCommand for Import { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + let imported_passwords_len = match self.format { + Format::Lprs => { + let vaults = Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?; + let vaults_len = vaults.vaults.len(); + + vault_manager.vaults.extend(vaults.vaults); + vault_manager.try_export()?; + vaults_len + } + Format::BitWarden => { + let vaults: BitWardenPasswords = serde_json::from_reader(File::open(&self.path)?)?; + let vaults_len = vaults.items.len(); + + vault_manager + .vaults + .extend(vaults.items.into_iter().map(Vault::from)); + vault_manager.try_export()?; + vaults_len + } + }; + println!( + "{imported_passwords_len} vault{s} were imported successfully", + s = if imported_passwords_len >= 2 { "s" } else { "" } + ); + Ok(()) + } + + fn validate_args(&self) -> LprsResult<()> { if self.path.exists() { if self .path .extension() .is_some_and(|e| e.to_string_lossy().eq_ignore_ascii_case("json")) { - let imported_passwords_len = match self.format { - Format::Lprs => { - let vaults = - Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?; - let vaults_len = vaults.vaults.len(); - - vault_manager.vaults.extend(vaults.vaults); - vault_manager.try_export()?; - vaults_len - } - Format::BitWarden => { - let vaults: BitWardenPasswords = - serde_json::from_reader(File::open(&self.path)?)?; - let vaults_len = vaults.items.len(); - - vault_manager - .vaults - .extend(vaults.items.into_iter().map(Vault::from)); - vault_manager.try_export()?; - vaults_len - } - }; - println!( - "{imported_passwords_len} vault{s} were imported successfully", - s = if imported_passwords_len >= 2 { "s" } else { "" } - ); - Ok(()) } else { Err(LprsError::Io(IoError::new( -- 2.45.2