From b7628870e394cc91baced64f9849ab4fe1d3030e Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 19 Oct 2024 20:11:30 +0800 Subject: [PATCH] fix(CommandExt): invert success check --- src/cli/build.rs | 6 +----- src/cli/switch.rs | 12 ++---------- src/util/command_ext.rs | 6 +++--- src/util/mod.rs | 19 +++++++++++-------- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/cli/build.rs b/src/cli/build.rs index b7085b6..9feb52e 100644 --- a/src/cli/build.rs +++ b/src/cli/build.rs @@ -5,10 +5,6 @@ use owo_colors::OwoColorize as _; use crate::stages; use crate::util; -fn default_nom() -> bool { - util::which("nom").is_some() -} - #[derive(Parser, Debug)] pub struct BuildCommand { /// Path to a flake @@ -30,7 +26,7 @@ pub struct BuildCommand { impl super::Command for BuildCommand { fn action(&self, _global_options: &super::Cli) -> Result<()> { - let build_program = if self.nom.unwrap_or_else(default_nom) { + let build_program = if self.nom.unwrap_or_else(util::nom_available) { "nom" } else { "nix" diff --git a/src/cli/switch.rs b/src/cli/switch.rs index d602ad7..2f3c941 100644 --- a/src/cli/switch.rs +++ b/src/cli/switch.rs @@ -9,14 +9,6 @@ fn default_profile() -> String { "/nix/var/nix/profiles/system".to_owned() } -fn default_nom() -> bool { - util::which("nom").is_some() -} - -fn default_nvd() -> bool { - util::which("nvd").is_some() -} - #[derive(Parser, Debug)] #[allow(clippy::struct_excessive_bools)] pub struct SwitchCommand { @@ -51,7 +43,7 @@ pub struct SwitchCommand { impl super::Command for SwitchCommand { fn action(&self, _global_options: &super::Cli) -> Result<()> { - let build_program = if self.nom.unwrap_or_else(default_nom) { + let build_program = if self.nom.unwrap_or_else(util::nom_available) { "nom" } else { "nix" @@ -82,7 +74,7 @@ impl super::Command for SwitchCommand { util::log::success(out.display().dimmed()); - if self.nvd.unwrap_or_else(default_nvd) { + if self.nvd.unwrap_or_else(util::nvd_available) { stages::diff(&out)?; } diff --git a/src/util/command_ext.rs b/src/util/command_ext.rs index 1231ddd..ace204d 100644 --- a/src/util/command_ext.rs +++ b/src/util/command_ext.rs @@ -1,4 +1,4 @@ -use eyre::{eyre, Result}; +use eyre::{Report, Result}; pub trait CommandExt { fn error_for_status(&mut self, msg: &str) -> Result; @@ -9,9 +9,9 @@ impl CommandExt for std::process::Command { let output = self.output()?; if output.status.success() { - Err(eyre!("{msg}")) - } else { Ok(output) + } else { + Err(Report::msg(msg.to_owned())) } } } diff --git a/src/util/mod.rs b/src/util/mod.rs index a22a5af..5802d22 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -23,17 +23,20 @@ pub fn hostname() -> Result { pub fn which(exe_name: impl AsRef) -> Option { env::var_os("PATH").and_then(|paths| { - env::split_paths(&paths).find_map(|dir| { - let full_path = dir.join(&exe_name); - if full_path.is_file() { - Some(full_path) - } else { - None - } - }) + env::split_paths(&paths) + .map(|d| d.join(&exe_name)) + .find(|f| f.is_file()) }) } +pub fn nom_available() -> bool { + which("nom").is_some() +} + +pub fn nvd_available() -> bool { + which("nvd").is_some() +} + pub fn safe_remove_file(path: impl AsRef) -> Result<()> { match fs::remove_file(path) { Ok(()) => Ok(()),