refact(env): should_print_debug_info

This commit is contained in:
steven-omaha
2024-04-07 18:01:10 +02:00
parent db4f16c785
commit 8850cab4a3
3 changed files with 25 additions and 13 deletions
+9 -2
View File
@@ -3,7 +3,7 @@ use std::process::{Command, ExitStatus};
use anyhow::{anyhow, ensure, Context, Result}; 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 /// Run the editor and pass the provided files as arguments. The workdir is set
/// to the parent of the first file. /// to the parent of the first file.
@@ -29,16 +29,23 @@ where
inner(&files) 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 /// # Errors
/// ///
/// This function will return an error if the command cannot be run or if it returns a non-zero /// 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. /// 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<()> { 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 let exit_status = cmd
.status() .status()
.with_context(|| format!("running command [{cmd:?}]"))?; .with_context(|| format!("running command [{cmd:?}]"))?;
let success = exit_status.success(); let success = exit_status.success();
ensure!( ensure!(
success, success,
+7 -10
View File
@@ -10,7 +10,7 @@ use const_format::formatcp;
use crate::args::{self, PackageAction}; use crate::args::{self, PackageAction};
use crate::backend::{Backend, Backends, ToDoPerBackend}; use crate::backend::{Backend, Backends, ToDoPerBackend};
use crate::cmd::run_edit_command; 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::path::{binary_in_path, get_absolutized_file_paths, get_group_dir};
use crate::search; use crate::search;
use crate::ui::get_user_confirmation; use crate::ui::get_user_confirmation;
@@ -571,16 +571,13 @@ fn find_groups_by_name<'a>(names: &[String], groups: &'a HashSet<Group>) -> Resu
#[allow(clippy::option_if_let_else)] #[allow(clippy::option_if_let_else)]
fn show_backend_query_error(error: &anyhow::Error, backend: &dyn Backend) { fn show_backend_query_error(error: &anyhow::Error, backend: &dyn Backend) {
let section = backend.get_section(); let section = backend.get_section();
match get_single_var("RUST_BACKTRACE") { if should_print_debug_info() {
Some(s) => { eprintln!("WARNING: skipping backend '{section}':");
if s == "1" || s == "full" { for err in error.chain() {
eprintln!("WARNING: skipping backend '{section}':"); eprintln!(" {err}");
for err in error.chain() {
eprintln!(" {err}");
}
}
} }
None => eprintln!("WARNING: skipping backend '{section}': {error}"), } else {
eprintln!("WARNING: skipping backend '{section}': {error}");
} }
} }
+9 -1
View File
@@ -10,6 +10,14 @@ fn check_vars_in_order(vars: &[&str]) -> Option<String> {
vars.iter().find_map(|v| var(v).ok()) vars.iter().find_map(|v| var(v).ok())
} }
pub fn get_single_var(variable: &str) -> Option<String> { fn get_single_var(variable: &str) -> Option<String> {
var(variable).ok() 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,
}
}