feat: Support removing multiple vaults in single command
All checks were successful
Rust CI / Rust CI (pull_request) Successful in 1m52s

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-20 17:01:11 +00:00
parent dbaa518855
commit bf8e6ca36a
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -19,14 +19,20 @@ use std::num::NonZeroUsize;
use clap::Args; use clap::Args;
use either::Either; 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)] #[derive(Debug, Args)]
/// Remove command, used to remove a vault from the vaults file /// Remove command, used to remove a vault from the vaults file
pub struct Remove { 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::<NonZeroUsize, String>)] #[arg(name = "INDEX-or-NAME", value_parser = either_parser::<NonZeroUsize, String>)]
location: Either<NonZeroUsize, String>, locations: Vec<Either<NonZeroUsize, String>>,
/// Force remove, will not return error if there is no vault with the given /// Force remove, will not return error if there is no vault with the given
/// index or name /// index or name
@ -36,16 +42,26 @@ pub struct Remove {
impl LprsCommand for Remove { impl LprsCommand for Remove {
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
let index = match utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults) { let indexes = self
Ok((idx, _)) => idx, .locations
.iter()
.map(|location| {
utils::vault_by_index_or_name(location, &mut vault_manager.vaults)
.map(|(_, v)| v.clone())
})
.collect::<LprsResult<Vec<Vault>>>();
match indexes {
Ok(indexes) => vault_manager.vaults.retain(|v| !indexes.contains(v)),
Err(err) => { Err(err) => {
if self.force { if self.force {
log::warn!("Ignoring error: {err}");
return Ok(()); return Ok(());
} }
return Err(err); return Err(err);
} }
}; }
vault_manager.vaults.remove(index);
vault_manager.try_export() vault_manager.try_export()
} }
} }