diff --git a/crates/pacdef_core/src/cmd.rs b/crates/pacdef_core/src/cmd.rs index 0725cc1..6cc5621 100644 --- a/crates/pacdef_core/src/cmd.rs +++ b/crates/pacdef_core/src/cmd.rs @@ -1,19 +1,23 @@ -use std::path::PathBuf; +use std::path::Path; use std::process::{Command, ExitStatus}; use anyhow::{anyhow, Context, Result}; use crate::env::get_editor; -pub fn run_edit_command(files: &[PathBuf]) -> Result { +pub fn run_edit_command

(files: &[P]) -> Result +where + P: for<'a> AsRef<&'a Path>, +{ let mut cmd = Command::new(get_editor().context("getting suitable editor")?); cmd.current_dir( files[0] + .as_ref() .parent() .context("getting parent dir of first file argument")?, ); for f in files { - cmd.arg(f.to_string_lossy().to_string()); + cmd.arg(f.as_ref().to_string_lossy().to_string()); } cmd.status().map_err(|e| anyhow!(e)) } diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 50e52d9..9173489 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -3,7 +3,7 @@ use std::fs::{remove_file, File}; use std::os::unix::fs::symlink; use std::path::PathBuf; -use anyhow::{anyhow, ensure, Context, Result}; +use anyhow::{anyhow, bail, ensure, Context, Result}; use clap::ArgMatches; use crate::action::*; @@ -124,27 +124,36 @@ impl Pacdef { #[allow(clippy::unused_self)] fn edit_group_files(&self, groups: &ArgMatches) -> Result<()> { - let group_dir = crate::path::get_group_dir()?; - - let files: Vec<_> = groups - .get_many::("group") - .context("getting group from args")? - .map(|file| { - let mut buf = group_dir.clone(); - buf.push(file); - buf + let paths: Vec<_> = self + .groups + .iter() + .map(|g| { + g.path + .file_name() + .expect("group files do not terminate in `..`") }) .collect(); - for file in &files { - ensure!( - file.exists(), - "group file {} not found", - file.to_string_lossy() - ); + let file_names: Vec<_> = groups + .get_many::("group") + .context("getting group from args")? + .collect(); + + let mut filtered_groups = Vec::new(); + + 'outer: for file_name in &file_names { + for group in &self.groups { + if **file_name == group.name { + filtered_groups.push(group); + break 'outer; + } + } + bail!("group file {} not found", file_name); } - let success = run_edit_command(&files) + let group_files: Vec<_> = filtered_groups.into_iter().map(|g| &g.path).collect(); + + let success = run_edit_command(&group_files) .context("running editor")? .success();