From 509acf8a22656c6d663243ee3af90bd49927800f Mon Sep 17 00:00:00 2001 From: Anssi Piirainen Date: Sun, 12 May 2024 22:35:02 +0300 Subject: [PATCH] Refactor code for improved grep handling - Refactor several functions to improve the handling of the grep option. - Remove several unnecessary functions in the code to streamline the deletion and showing process. --- src/actions/delete.rs | 34 ++++++---------------------- src/actions/mod.rs | 2 +- src/actions/show.rs | 46 +++++++++++++------------------------- src/vault/keepass_vault.rs | 8 +++---- src/vault/vault_trait.rs | 4 ++-- 5 files changed, 30 insertions(+), 64 deletions(-) diff --git a/src/actions/delete.rs b/src/actions/delete.rs index 29df825..a778c51 100644 --- a/src/actions/delete.rs +++ b/src/actions/delete.rs @@ -186,26 +186,6 @@ impl DeleteAction { } } -fn delete_credentials(vault: &mut Box, grep: &str) { - let matches = vault.grep(&Some(String::from(grep))); - handle_matches(matches, &mut Box::new(DeleteCredentialsTemplate { vault, grep })); -} - -fn delete_payment(vault: &mut Box) { - let cards = vault.find_payments(); - handle_matches(cards, &mut Box::new(DeletePaymentTemplate { vault })); -} - -fn delete_note(vault: &mut Box) { - let notes = vault.find_notes(); - handle_matches(notes, &mut Box::new(DeleteNoteTemplate { vault })); -} - -fn delete_totp(vault: &mut Box) { - let totps = vault.find_totp(&None); - handle_matches(totps, &mut Box::new(DeleteTotpTemplate { vault })); -} - impl UnlockingAction for DeleteAction { fn is_totp_vault(&self) -> bool { self.is_totp @@ -214,20 +194,20 @@ impl UnlockingAction for DeleteAction { fn run_with_vault(&self, vault: &mut Box) -> anyhow::Result<()> { match self.item_type { ItemType::Credential => { - let grep = match &self.grep { - Some(grep) => grep, - None => panic!("-g is required"), + let grep = match &self.grep { + Some(grep) => grep.as_str(), + None => return Err(anyhow::anyhow!("REGEXP is required for credentials")), }; - delete_credentials(vault, grep); + handle_matches(vault.grep(Some(grep)), &mut Box::new(DeleteCredentialsTemplate { vault, grep })); } ItemType::Payment => { - delete_payment(vault); + handle_matches(vault.find_payments(), &mut Box::new(DeletePaymentTemplate { vault })); } ItemType::Note => { - delete_note(vault); + handle_matches(vault.find_notes(), &mut Box::new(DeleteNoteTemplate { vault })); } ItemType::Totp => { - delete_totp(vault); + handle_matches(vault.find_totp(None), &mut Box::new(DeleteTotpTemplate { vault })); } }; Ok(()) diff --git a/src/actions/mod.rs b/src/actions/mod.rs index 36bfcf5..31b4abe 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -154,7 +154,7 @@ impl ExportAction { pub fn export_csv(&self, vault: &mut Box) -> anyhow::Result { debug!("exporting to csv"); if self.item_type == ItemType::Credential { - let creds = vault.grep(&None); + let creds = vault.grep(None); if creds.is_empty() { println!("No credentials found"); return Ok(0); diff --git a/src/actions/show.rs b/src/actions/show.rs index 39f6d17..709fc4f 100644 --- a/src/actions/show.rs +++ b/src/actions/show.rs @@ -180,32 +180,6 @@ impl ShowAction { is_totp: matches.get_one::("otp").map_or(false, |v| *v), } } - - fn show_credentials(&self, vault: &mut Box) { - let grep = match &self.grep { - Some(grep) => Some(String::from(grep)), - None => panic!("-g is required"), - }; - let matches = vault.grep(&grep); - handle_matches(matches, &mut Box::new(ShowCredentialsTemplate { verbose: self.verbose })); - } - - fn show_payments(&self, vault: &mut Box) { - debug!("showing payments"); - let matches = vault.find_payments(); - handle_matches(matches, &mut Box::new(ShowPaymentsTemplate { show_cleartext: self.verbose })); - } - - fn show_notes(&self, vault: &mut Box) { - debug!("showing notes"); - let matches = vault.find_notes(); - handle_matches(matches, &mut Box::new(ShowNotesTemplate { verbose: self.verbose })); - } - - fn show_totps(&self, vault: &mut Box) { - let totps = vault.find_totp(&self.grep); - handle_matches(totps, &mut Box::new(ShowTotpTemplate)); - } } impl UnlockingAction for ShowAction { @@ -215,10 +189,22 @@ impl UnlockingAction for ShowAction { fn run_with_vault(&self, vault: &mut Box) -> anyhow::Result<()> { match self.item_type { - ItemType::Credential => self.show_credentials(vault), - ItemType::Payment => self.show_payments(vault), - ItemType::Note => self.show_notes(vault), - ItemType::Totp => self.show_totps(vault) + ItemType::Credential => { + let grep = match &self.grep { + Some(grep) => grep.as_str(), + None => return Err(anyhow::anyhow!("REGEXP is required for credentials")), + }; + handle_matches(vault.grep(Some(grep)), &mut Box::new(ShowCredentialsTemplate { verbose: self.verbose })); + } + ItemType::Payment => { + handle_matches(vault.find_payments(), &mut Box::new(ShowPaymentsTemplate { show_cleartext: self.verbose })); + } + ItemType::Note => { + handle_matches(vault.find_notes(), &mut Box::new(ShowNotesTemplate { verbose: self.verbose })); + } + ItemType::Totp => { + handle_matches(vault.find_totp(None), &mut Box::new(ShowTotpTemplate)); + } }; Ok(()) } diff --git a/src/vault/keepass_vault.rs b/src/vault/keepass_vault.rs index 6f406ac..5b94b7b 100644 --- a/src/vault/keepass_vault.rs +++ b/src/vault/keepass_vault.rs @@ -114,7 +114,7 @@ impl KeepassVault { Ok((db_file, key)) } - fn load_credentials(&self, grep: &Option) -> Vec { + fn load_credentials(&self, grep: Option<&str>) -> Vec { NodeIterator::new(&self.get_root()) .filter(node_is_entry) .map(Self::node_to_credential) @@ -128,7 +128,7 @@ impl KeepassVault { }).collect() } - fn load_totps(&self, grep: &Option) -> Vec { + fn load_totps(&self, grep: Option<&str>) -> Vec { NodeIterator::new(&self.get_root()) // .map(|node| {debug!("Node: {:?}", node); node}) .filter(node_is_entry) @@ -374,7 +374,7 @@ impl PasswordVault for KeepassVault { self.password.clone() } - fn grep(&self, grep: &Option) -> Vec { + fn grep(&self, grep: Option<&str>) -> Vec { self.load_credentials(grep) } @@ -448,7 +448,7 @@ impl NoteVault for KeepassVault { } impl TotpVault for KeepassVault { - fn find_totp(&self, grep: &Option) -> Vec { + fn find_totp(&self, grep: Option<&str>) -> Vec { self.load_totps(grep) } diff --git a/src/vault/vault_trait.rs b/src/vault/vault_trait.rs index d33221f..4e92ffe 100644 --- a/src/vault/vault_trait.rs +++ b/src/vault/vault_trait.rs @@ -4,7 +4,7 @@ use crate::vault::entities::{Credential, Note, PaymentCard, Totp}; pub trait PasswordVault { fn get_master_password(&self) -> String; - fn grep(&self, grep: &Option) -> Vec; + fn grep(&self, grep: Option<&str>) -> Vec; fn save_credentials( &mut self, @@ -47,7 +47,7 @@ pub trait NoteVault { } pub trait TotpVault { - fn find_totp(&self, grep: &Option) -> Vec; + fn find_totp(&self, grep: Option<&str>) -> Vec; fn save_totp(&mut self, totp: &Totp) -> i8;