From 31a68b927764a7eb0b38539f630b70fa258ae7aa Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 30 Apr 2024 16:49:21 +0200 Subject: [PATCH] feat: Support `--verbose` flag (#23) Reviewed-on: https://git.4rs.nl/awiteb/lprs/pulls/23 Co-authored-by: Awiteb Co-committed-by: Awiteb --- src/cli/add_command.rs | 6 +++++- src/cli/clean_command.rs | 4 ++++ src/cli/edit_command.rs | 2 ++ src/cli/export_command.rs | 6 ++++++ src/cli/import_command.rs | 7 +++++++ src/cli/list_command.rs | 4 ++++ src/cli/mod.rs | 18 ++++++++++++------ src/cli/remove_command.rs | 6 ++++++ src/main.rs | 11 +++++++++-- src/utils.rs | 6 ++++++ src/vault/mod.rs | 4 ++++ 11 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index dc7f5ef..3f5a698 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -34,8 +34,12 @@ pub struct Add { impl LprsCommand for Add { fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { match self.password { - Some(Some(password)) => self.vault_info.password = Some(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( inquire::Password::new("Vault password:") .without_confirmation() diff --git a/src/cli/clean_command.rs b/src/cli/clean_command.rs index 989bf1d..bc29626 100644 --- a/src/cli/clean_command.rs +++ b/src/cli/clean_command.rs @@ -29,6 +29,10 @@ pub struct Clean {} impl LprsCommand for Clean { fn run(self, vault_manager: Vaults) -> LprsResult<()> { + log::info!( + "Cleaning the vaults file: {:?}", + vault_manager.vaults_file.display() + ); 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 9fe9b17..443f248 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -49,6 +49,7 @@ pub struct Edit { impl LprsCommand for Edit { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = self.index.get() as usize; + log::debug!("Editing vault at index: {index}"); let Some(vault) = vault_manager.vaults.get_mut(index - 1) else { return Err(LprsError::InvalidVaultIndex(format!( @@ -71,6 +72,7 @@ impl LprsCommand for Edit { 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()), diff --git a/src/cli/export_command.rs b/src/cli/export_command.rs index 56f18f5..fb5d701 100644 --- a/src/cli/export_command.rs +++ b/src/cli/export_command.rs @@ -35,6 +35,12 @@ pub struct Export { impl LprsCommand for Export { fn run(self, vault_manager: Vaults) -> LprsResult<()> { + log::debug!( + "Exporting vault {} to: {} with format: {}", + vault_manager.vaults_file.display(), + self.path.display(), + self.format + ); let exported_data = match self.format { Format::Lprs => { serde_json::to_string::>>(&vault_manager.encrypt_vaults()?) diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index 6c46762..1e2b098 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -36,6 +36,13 @@ pub struct Import { impl LprsCommand for Import { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + log::debug!( + "Importing vaults from: {} with format: {} to the vault: {}", + self.path.display(), + self.format, + vault_manager.vaults_file.display() + ); + let imported_passwords_len = match self.format { Format::Lprs => { let vaults = Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?; diff --git a/src/cli/list_command.rs b/src/cli/list_command.rs index 48ad82a..266f426 100644 --- a/src/cli/list_command.rs +++ b/src/cli/list_command.rs @@ -46,6 +46,7 @@ impl LprsCommand for List { )); } if let Some(user_vault_index) = self.get.map(|n| (n.get() - 1) as usize) { + log::info!("Getting the vault at index: {user_vault_index}"); if user_vault_index >= vault_manager.vaults.len() { return Err(LprsError::Other( "The `--get` index is great then the vaults length".to_owned(), @@ -67,6 +68,7 @@ impl LprsCommand for List { regex::escape(self.filter.as_deref().unwrap_or("")) ) }; + log::debug!("Listing vaults filtered by: {pattern}"); let re = regex::Regex::new(&pattern)?; @@ -107,6 +109,8 @@ impl LprsCommand for List { .parse::() .unwrap_or_default(); + log::debug!("The user selected the vault at index: {vault_idx}"); + println!( "{}", vault_manager diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 571ee34..4a0a1dd 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -58,33 +58,39 @@ impl_commands!(Commands, Add Remove List Clean Edit Gen Export Import); #[command(author, version, about, long_about = None)] pub struct Cli { /// The vaults json file + #[arg(short = 'f', long)] + pub vaults_file: Option, + /// Show the logs in the stdout #[arg(short, long)] - vaults_file: Option, + pub verbose: bool, - // TODO: verbose flag #[command(subcommand)] - command: Commands, + pub command: Commands, } impl Cli { /// Run the cli pub fn run(self) -> LprsResult<()> { - let vaults_file = if let Some(ref path) = self.vaults_file { - path.clone() + let vaults_file = if let Some(path) = self.vaults_file { + log::info!("Using the given vaults file"); + path } else { + log::info!("Using the default vaults file"); crate::utils::vaults_file()? }; - log::debug!("Getting the vaults file: {}", vaults_file.to_string_lossy()); + log::debug!("Vaults file: {}", vaults_file.display()); self.command.validate_args()?; let vault_manager = if matches!(self.command, Commands::Clean(..) | Commands::Gen(..)) { + log::info!("Running command that don't need the vault manager"); // Returns empty vault manager for those commands don't need it Vaults { vaults_file, ..Default::default() } } else { + log::info!("Reloading the vaults file"); let master_password = utils::master_password_prompt(&vaults_file)?; Vaults::try_reload( vaults_file, diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs index af0ef12..d255e5b 100644 --- a/src/cli/remove_command.rs +++ b/src/cli/remove_command.rs @@ -37,11 +37,17 @@ pub struct Remove { impl LprsCommand for Remove { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = (self.index.get() - 1) as usize; + log::debug!("Removing vault at index: {index}"); + if index > vault_manager.vaults.len() { if !self.force { return Err(LprsError::Other( "The index is greater than the passwords counts".to_owned(), )); + } else { + log::error!( + "The index is greater than the passwords counts, but the force flag is enabled" + ); } } else { vault_manager.vaults.remove(index); diff --git a/src/main.rs b/src/main.rs index e7ee365..5107b9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,10 +43,15 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const LAST_VERSION_CHECK_FILE: &str = ".last_version_check"; fn main() -> ExitCode { + let lprs_cli = cli::Cli::parse(); + if lprs_cli.verbose { + std::env::set_var("RUST_LOG", "lprs"); + } pretty_env_logger::init(); #[cfg(feature = "update-notify")] { + log::info!("Checking for new version of lprs..."); match utils::lprs_version() { Ok(Some(new_version)) if new_version != VERSION => { println!( @@ -60,11 +65,13 @@ fn main() -> ExitCode { eprintln!("{err}"); return ExitCode::FAILURE; } - _ => {} + _ => { + log::info!("No new version found."); + } } } - if let Err(err) = cli::Cli::parse().run() { + if let Err(err) = lprs_cli.run() { if !matches!( err, LprsError::Inquire(InquireError::OperationCanceled) diff --git a/src/utils.rs b/src/utils.rs index c78e087..f88065d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -30,7 +30,9 @@ pub fn local_project_file(filename: &str) -> LprsResult { .ok_or_else(|| { LprsError::ProjectDir("Can't extract the project_dir from this OS".to_owned()) })?; + log::debug!("Local project dir: {:?}", local_dir.display()); if !local_dir.exists() { + log::info!("Creating the local project dir: {:?}", local_dir.display()); fs::create_dir_all(&local_dir)?; } Ok(local_dir.join(filename)) @@ -40,6 +42,10 @@ pub fn local_project_file(filename: &str) -> LprsResult { pub fn vaults_file() -> LprsResult { let vaults_file = local_project_file(crate::DEFAULT_VAULTS_FILE)?; if !vaults_file.exists() { + log::info!( + "Vaults file not found, creating a new one: {:?}", + vaults_file.display() + ); fs::write(&vaults_file, "[]")?; } Ok(vaults_file) diff --git a/src/vault/mod.rs b/src/vault/mod.rs index bd6b720..a7d2fef 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -190,6 +190,10 @@ impl Vaults { /// Encrypt the vaults then export it to the file pub fn try_export(self) -> LprsResult<()> { + log::debug!( + "Trying to export the vaults to the file: {}", + self.vaults_file.display() + ); fs::write( &self.vaults_file, serde_json::to_string(&self.encrypt_vaults()?)?,