From d45cdc4eaeb88feeb874b94bc3eac87e38eb541d Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Thu, 23 Feb 2023 22:02:15 +0100 Subject: [PATCH] fix 'group new' subcommand --- crates/pacdef_core/src/cmd.rs | 28 ++++++++++++++++++---------- crates/pacdef_core/src/core.rs | 19 +++++++++++++++++-- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/crates/pacdef_core/src/cmd.rs b/crates/pacdef_core/src/cmd.rs index 0917e28..695c61d 100644 --- a/crates/pacdef_core/src/cmd.rs +++ b/crates/pacdef_core/src/cmd.rs @@ -7,15 +7,23 @@ use crate::env::get_editor; /// Run the editor and pass the provided files as arguments. The workdir is set /// to the parent of the first file. -pub fn run_edit_command(files: &[&Path]) -> Result { - let mut cmd = Command::new(get_editor().context("getting suitable editor")?); - cmd.current_dir( - files[0] - .parent() - .context("getting parent dir of first file argument")?, - ); - for f in files { - cmd.arg(f.to_string_lossy().to_string()); +pub fn run_edit_command

(files: &[P]) -> Result +where + P: AsRef, +{ + fn inner(files: &[&Path]) -> Result { + let mut cmd = Command::new(get_editor().context("getting suitable editor")?); + cmd.current_dir( + files[0] + .parent() + .context("getting parent dir of first file argument")?, + ); + for f in files { + cmd.arg(f.to_string_lossy().to_string()); + } + cmd.status().map_err(|e| anyhow!(e)) } - cmd.status().map_err(|e| anyhow!(e)) + + let files: Vec<_> = files.iter().map(|p| p.as_ref()).collect(); + inner(&files) } diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index d2d1595..e25048e 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -280,8 +280,23 @@ impl Pacdef { Ok(()) } + #[allow(clippy::unused_self)] fn new_groups(&self, arg_matches: &ArgMatches) -> Result<()> { - let paths = get_group_file_paths_matching_args(arg_matches, &self.groups)?; + let group_path = get_group_dir()?; + + let new_group_names: Vec<_> = arg_matches + .get_many::("groups") + .context("getting groups from args")? + .collect(); + + let paths: Vec<_> = new_group_names + .into_iter() + .map(|name| { + let mut base = group_path.clone(); + base.push(name); + base + }) + .collect(); for file in &paths { ensure!(!file.exists(), "group already exists under {file:?}"); @@ -323,7 +338,7 @@ fn get_group_file_paths_matching_args<'a>( groups: &'a HashSet, ) -> Result> { let file_names: Vec<_> = arg_match - .get_many::("group") + .get_many::("groups") .context("getting groups from args")? .collect();