From bf8e6ca36a130f6cca81bb6e91a9f5b83624f1bf Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 20 Aug 2024 17:01:11 +0000 Subject: [PATCH] feat: Support removing multiple vaults in single command Signed-off-by: Awiteb --- src/cli/remove_command.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs index d0a904d..7281e0c 100644 --- a/src/cli/remove_command.rs +++ b/src/cli/remove_command.rs @@ -19,14 +19,20 @@ use std::num::NonZeroUsize; use clap::Args; use either::Either; -use crate::{clap_parsers::either_parser, utils, vault::Vaults, LprsCommand, LprsResult}; +use crate::{ + clap_parsers::either_parser, + utils, + vault::{Vault, Vaults}, + LprsCommand, + LprsResult, +}; #[derive(Debug, Args)] /// Remove command, used to remove a vault from the vaults file pub struct Remove { - /// The vault to remove, index or name + /// The vaults to remove, index or name #[arg(name = "INDEX-or-NAME", value_parser = either_parser::)] - location: Either, + locations: Vec>, /// Force remove, will not return error if there is no vault with the given /// index or name @@ -36,16 +42,26 @@ pub struct Remove { impl LprsCommand for Remove { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { - let index = match utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults) { - Ok((idx, _)) => idx, + let indexes = self + .locations + .iter() + .map(|location| { + utils::vault_by_index_or_name(location, &mut vault_manager.vaults) + .map(|(_, v)| v.clone()) + }) + .collect::>>(); + + match indexes { + Ok(indexes) => vault_manager.vaults.retain(|v| !indexes.contains(v)), Err(err) => { if self.force { + log::warn!("Ignoring error: {err}"); return Ok(()); } return Err(err); } - }; - vault_manager.vaults.remove(index); + } + vault_manager.try_export() } } -- 2.45.2