diff --git a/crates/pacdef_core/src/cmd.rs b/crates/pacdef_core/src/cmd.rs index 9806165..61bb8f9 100644 --- a/crates/pacdef_core/src/cmd.rs +++ b/crates/pacdef_core/src/cmd.rs @@ -3,7 +3,7 @@ use std::process::{Command, ExitStatus}; use anyhow::{anyhow, ensure, Context, Result}; -use crate::env::get_editor; +use crate::env::{get_editor, should_print_debug_info}; /// Run the editor and pass the provided files as arguments. The workdir is set /// to the parent of the first file. @@ -29,16 +29,23 @@ where inner(&files) } -/// Run an external command. Use the anyhow framework to bubble up errors if they occur. +/// Run an external command. Use the anyhow framework to bubble up errors if they occur. Will print +/// the full command to be executed when pacdef is in debug mode. /// /// # Errors /// /// This function will return an error if the command cannot be run or if it returns a non-zero /// exit status. In case of an error the full command will be part of the error message. pub fn run_external_command(mut cmd: Command) -> Result<()> { + if should_print_debug_info() { + println!("will run the following command"); + dbg!(&cmd); + } + let exit_status = cmd .status() .with_context(|| format!("running command [{cmd:?}]"))?; + let success = exit_status.success(); ensure!( success, diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index b79b6e8..8e3e4c4 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -10,7 +10,7 @@ use const_format::formatcp; use crate::args::{self, PackageAction}; use crate::backend::{Backend, Backends, ToDoPerBackend}; use crate::cmd::run_edit_command; -use crate::env::get_single_var; +use crate::env::should_print_debug_info; use crate::path::{binary_in_path, get_absolutized_file_paths, get_group_dir}; use crate::search; use crate::ui::get_user_confirmation; @@ -571,16 +571,13 @@ fn find_groups_by_name<'a>(names: &[String], groups: &'a HashSet) -> Resu #[allow(clippy::option_if_let_else)] fn show_backend_query_error(error: &anyhow::Error, backend: &dyn Backend) { let section = backend.get_section(); - match get_single_var("RUST_BACKTRACE") { - Some(s) => { - if s == "1" || s == "full" { - eprintln!("WARNING: skipping backend '{section}':"); - for err in error.chain() { - eprintln!(" {err}"); - } - } + if should_print_debug_info() { + eprintln!("WARNING: skipping backend '{section}':"); + for err in error.chain() { + eprintln!(" {err}"); } - None => eprintln!("WARNING: skipping backend '{section}': {error}"), + } else { + eprintln!("WARNING: skipping backend '{section}': {error}"); } } diff --git a/crates/pacdef_core/src/env.rs b/crates/pacdef_core/src/env.rs index 4172f1b..6991e37 100644 --- a/crates/pacdef_core/src/env.rs +++ b/crates/pacdef_core/src/env.rs @@ -10,6 +10,14 @@ fn check_vars_in_order(vars: &[&str]) -> Option { vars.iter().find_map(|v| var(v).ok()) } -pub fn get_single_var(variable: &str) -> Option { +fn get_single_var(variable: &str) -> Option { var(variable).ok() } + +pub fn should_print_debug_info() -> bool { + match get_single_var("RUST_BACKTRACE") { + Some(value) if ["s", "full"].contains(&value.as_str()) => true, + Some(_) => false, + None => false, + } +}