handle errors in editing files
This commit is contained in:
+7
-4
@@ -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<ExitStatus> {
|
||||
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<Package>) {
|
||||
|
||||
+10
-5
@@ -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<HashSet<Group>>,
|
||||
@@ -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::<String>("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) {
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
use std::env::var;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub(crate) fn get_editor() -> Result<String> {
|
||||
check_vars_in_order(&["EDITOR", "VISUAL"]).ok_or_else(|| anyhow!("could not find editor"))
|
||||
}
|
||||
|
||||
fn check_vars_in_order(vars: &[&str]) -> Option<String> {
|
||||
vars.iter().flat_map(|v| var(v).ok()).next()
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub mod args;
|
||||
mod cmd;
|
||||
mod core;
|
||||
pub mod db;
|
||||
mod env;
|
||||
mod group;
|
||||
mod package;
|
||||
mod path;
|
||||
|
||||
Reference in New Issue
Block a user