refactor 'run_edit_command'
This commit is contained in:
@@ -1,19 +1,23 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::Path;
|
||||||
use std::process::{Command, ExitStatus};
|
use std::process::{Command, ExitStatus};
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
|
||||||
use crate::env::get_editor;
|
use crate::env::get_editor;
|
||||||
|
|
||||||
pub fn run_edit_command(files: &[PathBuf]) -> Result<ExitStatus> {
|
pub fn run_edit_command<P>(files: &[P]) -> Result<ExitStatus>
|
||||||
|
where
|
||||||
|
P: for<'a> AsRef<&'a Path>,
|
||||||
|
{
|
||||||
let mut cmd = Command::new(get_editor().context("getting suitable editor")?);
|
let mut cmd = Command::new(get_editor().context("getting suitable editor")?);
|
||||||
cmd.current_dir(
|
cmd.current_dir(
|
||||||
files[0]
|
files[0]
|
||||||
|
.as_ref()
|
||||||
.parent()
|
.parent()
|
||||||
.context("getting parent dir of first file argument")?,
|
.context("getting parent dir of first file argument")?,
|
||||||
);
|
);
|
||||||
for f in files {
|
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))
|
cmd.status().map_err(|e| anyhow!(e))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::fs::{remove_file, File};
|
|||||||
use std::os::unix::fs::symlink;
|
use std::os::unix::fs::symlink;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::{anyhow, ensure, Context, Result};
|
use anyhow::{anyhow, bail, ensure, Context, Result};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
|
||||||
use crate::action::*;
|
use crate::action::*;
|
||||||
@@ -124,27 +124,36 @@ impl Pacdef {
|
|||||||
|
|
||||||
#[allow(clippy::unused_self)]
|
#[allow(clippy::unused_self)]
|
||||||
fn edit_group_files(&self, groups: &ArgMatches) -> Result<()> {
|
fn edit_group_files(&self, groups: &ArgMatches) -> Result<()> {
|
||||||
let group_dir = crate::path::get_group_dir()?;
|
let paths: Vec<_> = self
|
||||||
|
.groups
|
||||||
let files: Vec<_> = groups
|
.iter()
|
||||||
.get_many::<String>("group")
|
.map(|g| {
|
||||||
.context("getting group from args")?
|
g.path
|
||||||
.map(|file| {
|
.file_name()
|
||||||
let mut buf = group_dir.clone();
|
.expect("group files do not terminate in `..`")
|
||||||
buf.push(file);
|
|
||||||
buf
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for file in &files {
|
let file_names: Vec<_> = groups
|
||||||
ensure!(
|
.get_many::<String>("group")
|
||||||
file.exists(),
|
.context("getting group from args")?
|
||||||
"group file {} not found",
|
.collect();
|
||||||
file.to_string_lossy()
|
|
||||||
);
|
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")?
|
.context("running editor")?
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user