From 7fb416815cf2303311d915fcf19109a17d4655b5 Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Fri, 2 Dec 2022 17:58:53 +0100 Subject: [PATCH] start propagating errors --- src/core.rs | 23 +++++++++++++---------- src/main.rs | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/core.rs b/src/core.rs index daa744b..7001cc7 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::process::exit; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use clap::ArgMatches; use crate::action; @@ -61,13 +61,13 @@ impl Pacdef { run_install_command(diff); } - pub fn run_action_from_arg(self) { + pub fn run_action_from_arg(self) -> Result<()> { match self.args.subcommand() { - 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(), - Some((action::VERSION, _)) => self.show_version(), + Some((action::EDIT, groups)) => self.edit_group_files(groups).context("editing"), + Some((action::GROUPS, _)) => Ok(self.show_groups()), + Some((action::SYNC, _)) => Ok(self.install_packages()), + Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()), + Some((action::VERSION, _)) => Ok(self.show_version()), _ => todo!(), } } @@ -75,17 +75,20 @@ impl Pacdef { pub(crate) fn edit_group_files(&self, groups: &ArgMatches) -> Result<()> { let files: Vec<_> = groups .get_many::("group") - .unwrap() + .context("getting group from args")? .map(|file| { let mut buf = crate::path::get_pacdef_group_dir().unwrap(); buf.push(file); buf }) .collect(); - if run_edit_command(&files)?.success() { + if run_edit_command(&files) + .context("running editor")? + .success() + { Ok(()) } else { - bail!("command exited with error") + bail!("editor exited with error") } } diff --git a/src/main.rs b/src/main.rs index f4efb0e..bfbf1d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use pacdef::{args, Group, Pacdef}; @@ -6,6 +6,6 @@ fn main() -> Result<()> { let args = args::get_args(); let groups = Group::load_from_dir(); let pacdef = Pacdef::new(args, groups); - pacdef.run_action_from_arg(); + pacdef.run_action_from_arg().context("running action")?; Ok(()) }