Skip to content

Commit

Permalink
Refactor code for improved grep handling
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
anssip committed May 12, 2024
1 parent aa98bdb commit 509acf8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 64 deletions.
34 changes: 7 additions & 27 deletions src/actions/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,6 @@ impl DeleteAction {
}
}

fn delete_credentials(vault: &mut Box<dyn Vault>, 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<dyn Vault>) {
let cards = vault.find_payments();
handle_matches(cards, &mut Box::new(DeletePaymentTemplate { vault }));
}

fn delete_note(vault: &mut Box<dyn Vault>) {
let notes = vault.find_notes();
handle_matches(notes, &mut Box::new(DeleteNoteTemplate { vault }));
}

fn delete_totp(vault: &mut Box<dyn Vault>) {
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
Expand All @@ -214,20 +194,20 @@ impl UnlockingAction for DeleteAction {
fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<()> {
match self.item_type {
ItemType::Credential => {
let grep = match &self.grep {
Some(grep) => grep,
None => panic!("-g <REGEXP> 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(())
Expand Down
2 changes: 1 addition & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ExportAction {
pub fn export_csv(&self, vault: &mut Box<dyn Vault>) -> anyhow::Result<i64> {
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);
Expand Down
46 changes: 16 additions & 30 deletions src/actions/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,6 @@ impl ShowAction {
is_totp: matches.get_one::<bool>("otp").map_or(false, |v| *v),
}
}

fn show_credentials(&self, vault: &mut Box<dyn Vault>) {
let grep = match &self.grep {
Some(grep) => Some(String::from(grep)),
None => panic!("-g <REGEXP> is required"),
};
let matches = vault.grep(&grep);
handle_matches(matches, &mut Box::new(ShowCredentialsTemplate { verbose: self.verbose }));
}

fn show_payments(&self, vault: &mut Box<dyn Vault>) {
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<dyn Vault>) {
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<dyn Vault>) {
let totps = vault.find_totp(&self.grep);
handle_matches(totps, &mut Box::new(ShowTotpTemplate));
}
}

impl UnlockingAction for ShowAction {
Expand All @@ -215,10 +189,22 @@ impl UnlockingAction for ShowAction {

fn run_with_vault(&self, vault: &mut Box<dyn Vault>) -> 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(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/vault/keepass_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl KeepassVault {
Ok((db_file, key))
}

fn load_credentials(&self, grep: &Option<String>) -> Vec<Credential> {
fn load_credentials(&self, grep: Option<&str>) -> Vec<Credential> {
NodeIterator::new(&self.get_root())
.filter(node_is_entry)
.map(Self::node_to_credential)
Expand All @@ -128,7 +128,7 @@ impl KeepassVault {
}).collect()
}

fn load_totps(&self, grep: &Option<String>) -> Vec<Totp> {
fn load_totps(&self, grep: Option<&str>) -> Vec<Totp> {
NodeIterator::new(&self.get_root())
// .map(|node| {debug!("Node: {:?}", node); node})
.filter(node_is_entry)
Expand Down Expand Up @@ -374,7 +374,7 @@ impl PasswordVault for KeepassVault {
self.password.clone()
}

fn grep(&self, grep: &Option<String>) -> Vec<Credential> {
fn grep(&self, grep: Option<&str>) -> Vec<Credential> {
self.load_credentials(grep)
}

Expand Down Expand Up @@ -448,7 +448,7 @@ impl NoteVault for KeepassVault {
}

impl TotpVault for KeepassVault {
fn find_totp(&self, grep: &Option<String>) -> Vec<Totp> {
fn find_totp(&self, grep: Option<&str>) -> Vec<Totp> {
self.load_totps(grep)
}

Expand Down
4 changes: 2 additions & 2 deletions src/vault/vault_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Vec<Credential>;
fn grep(&self, grep: Option<&str>) -> Vec<Credential>;

fn save_credentials(
&mut self,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub trait NoteVault {
}

pub trait TotpVault {
fn find_totp(&self, grep: &Option<String>) -> Vec<Totp>;
fn find_totp(&self, grep: Option<&str>) -> Vec<Totp>;

fn save_totp(&mut self, totp: &Totp) -> i8;

Expand Down

0 comments on commit 509acf8

Please sign in to comment.