feat: Support removing multiple vaults in single command (#66)
All checks were successful
Write changelog / write-changelog (push) Successful in 4s
Rust CI / Rust CI (push) Successful in 1m56s

Reviewed-on: https://git.4rs.nl///awiteb/lprs/pulls/66
Co-authored-by: Awiteb <a@4rs.nl>
Co-committed-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-20 19:04:53 +02:00 committed by awiteb
parent dbaa518855
commit 6293eadafb

View file

@ -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::<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
/// 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::<LprsResult<Vec<Vault>>>();
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()
}
}