Skip to content

Commit

Permalink
fix(CommandExt): invert success check
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Oct 19, 2024
1 parent 0a19bb6 commit b762887
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
6 changes: 1 addition & 5 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
12 changes: 2 additions & 10 deletions src/cli/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)?;
}

Expand Down
6 changes: 3 additions & 3 deletions src/util/command_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eyre::{eyre, Result};
use eyre::{Report, Result};

pub trait CommandExt {
fn error_for_status(&mut self, msg: &str) -> Result<std::process::Output>;
Expand All @@ -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()))
}
}
}
19 changes: 11 additions & 8 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ pub fn hostname() -> Result<String> {

pub fn which(exe_name: impl AsRef<Path>) -> Option<PathBuf> {
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<Path>) -> Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Expand Down

0 comments on commit b762887

Please sign in to comment.