From d3b4dbfa8aa67de30764d4e8964cdbdd527f2bac Mon Sep 17 00:00:00 2001 From: timeshifter Date: Wed, 11 Jan 2023 15:02:37 +0100 Subject: [PATCH] add remove --- src/action.rs | 1 + src/args.rs | 8 +++++++- src/core.rs | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/action.rs b/src/action.rs index e365702..177d09e 100644 --- a/src/action.rs +++ b/src/action.rs @@ -2,6 +2,7 @@ pub(crate) const CLEAN: &str = "clean"; pub(crate) const EDIT: &str = "edit"; pub(crate) const GROUPS: &str = "groups"; pub(crate) const IMPORT: &str = "import"; +pub(crate) const REMOVE: &str = "remove"; pub(crate) const SHOW: &str = "show"; pub(crate) const SYNC: &str = "sync"; pub(crate) const UNMANAGED: &str = "unmanaged"; diff --git a/src/args.rs b/src/args.rs index 87b2b3e..5c76a99 100644 --- a/src/args.rs +++ b/src/args.rs @@ -25,6 +25,12 @@ fn get_arg_parser() -> Command<'static> { .arg_required_else_help(true) .arg(Arg::new("files").multiple_values(true)), ) + .subcommand( + Command::new(REMOVE) + .about("remove one or more previously imported groups") + .arg_required_else_help(true) + .arg(Arg::new("groups").multiple_values(true)), + ) .subcommand( Command::new(SHOW) .about("show packages under an imported group") @@ -45,7 +51,7 @@ pub fn get() -> clap::ArgMatches { get_arg_parser().get_matches() } -pub(crate) fn get_file_paths(arg_match: &ArgMatches) -> Vec { +pub(crate) fn get_absolutized_file_paths(arg_match: &ArgMatches) -> Vec { arg_match .get_many::("files") .unwrap() diff --git a/src/core.rs b/src/core.rs index 08df291..5cda5c9 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,7 @@ use std::collections::HashSet; +use std::fs::remove_file; use std::os::unix::fs::symlink; +use std::path::PathBuf; use anyhow::{ensure, Context, Result}; use clap::ArgMatches; @@ -26,9 +28,7 @@ impl Pacdef { #[allow(clippy::unit_arg)] pub fn run_action_from_arg(self) -> Result<()> { - // TODO import // TODO new - // TODO remove // TODO review // TODO search match self.args.subcommand() { @@ -38,6 +38,7 @@ impl Pacdef { } Some((action::GROUPS, _)) => Ok(self.show_groups()), Some((action::IMPORT, files)) => self.import_groups(files).context("importing groups"), + Some((action::REMOVE, groups)) => self.remove_groups(groups).context("removing groups"), Some((action::SHOW, groups)) => { self.show_group_content(groups).context("showing groups") } @@ -173,7 +174,7 @@ impl Pacdef { } fn import_groups(&self, args: &ArgMatches) -> Result<()> { - let files = args::get_file_paths(args); + let files = args::get_absolutized_file_paths(args); let groups_dir = get_pacdef_group_dir()?; for target in files { @@ -196,6 +197,36 @@ impl Pacdef { Ok(()) } + + fn remove_groups(&self, arg_match: &ArgMatches) -> Result<()> { + let paths = get_assumed_group_file_names(arg_match)?; + + for file in paths.iter() { + ensure!(file.exists(), "did not find the group under {file:?}"); + } + + for file in paths { + remove_file(file)?; + } + + Ok(()) + } +} + +fn get_assumed_group_file_names(arg_match: &ArgMatches) -> Result> { + let groups_dir = get_pacdef_group_dir()?; + + let paths: Vec<_> = arg_match + .get_many::("groups") + .unwrap() + .map(|s| { + let mut possible_group_file = groups_dir.clone(); + possible_group_file.push(s); + possible_group_file + }) + .collect(); + + Ok(paths) } fn show_error(error: anyhow::Error, backend: Box) {