diff --git a/src/cmd.rs b/src/cmd.rs index d1cb5c1..46fad3c 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,15 +1,18 @@ use std::os::unix::process::CommandExt; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, ExitStatus}; +use anyhow::{anyhow, Result}; + +use crate::env::get_editor; use crate::Package; -pub fn run_edit_command(files: &[PathBuf]) { - let mut cmd = Command::new("nvim"); +pub fn run_edit_command(files: &[PathBuf]) -> Result { + let mut cmd = Command::new(get_editor()?); for f in files { cmd.arg(f.to_string_lossy().to_string()); } - cmd.exec(); + cmd.status().map_err(|e| anyhow!(e)) } pub fn run_install_command(diff: Vec) { diff --git a/src/core.rs b/src/core.rs index e522e3f..daa744b 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,14 +1,15 @@ use std::collections::HashSet; use std::process::exit; +use anyhow::{bail, Result}; +use clap::ArgMatches; + use crate::action; use crate::cmd::{run_edit_command, run_install_command}; use crate::db::{get_all_installed_packages, get_explicitly_installed_packages}; use crate::Group; use crate::Package; -use clap::ArgMatches; - pub struct Pacdef { pub(crate) args: ArgMatches, pub(crate) groups: Option>, @@ -62,7 +63,7 @@ impl Pacdef { pub fn run_action_from_arg(self) { match self.args.subcommand() { - Some((action::EDIT, groups)) => self.edit_group_files(groups), + Some((action::EDIT, groups)) => self.edit_group_files(groups).unwrap(), Some((action::GROUPS, _)) => self.show_groups(), Some((action::SYNC, _)) => self.install_packages(), Some((action::UNMANAGED, _)) => self.show_unmanaged_packages(), @@ -71,7 +72,7 @@ impl Pacdef { } } - pub(crate) fn edit_group_files(&self, groups: &ArgMatches) { + pub(crate) fn edit_group_files(&self, groups: &ArgMatches) -> Result<()> { let files: Vec<_> = groups .get_many::("group") .unwrap() @@ -81,7 +82,11 @@ impl Pacdef { buf }) .collect(); - run_edit_command(&files) + if run_edit_command(&files)?.success() { + Ok(()) + } else { + bail!("command exited with error") + } } pub(crate) fn show_version(self) { diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..029da38 --- /dev/null +++ b/src/env.rs @@ -0,0 +1,11 @@ +use std::env::var; + +use anyhow::{anyhow, Result}; + +pub(crate) fn get_editor() -> Result { + check_vars_in_order(&["EDITOR", "VISUAL"]).ok_or_else(|| anyhow!("could not find editor")) +} + +fn check_vars_in_order(vars: &[&str]) -> Option { + vars.iter().flat_map(|v| var(v).ok()).next() +} diff --git a/src/lib.rs b/src/lib.rs index 0eb615d..bab3e45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod args; mod cmd; mod core; pub mod db; +mod env; mod group; mod package; mod path;