Skip to content

Commit

Permalink
add a mandatory direnv version check
Browse files Browse the repository at this point in the history
use semver to parse the version
  • Loading branch information
shivaraj-bh committed Mar 19, 2024
1 parent df4f03d commit af3027b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nix_health/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ anyhow = { version = "1.0.75" }
colored = { version = "2.0" }
which = { version = "4.4.2" }
bytesize.workspace = true
semver = { version = "1.0.22" }
52 changes: 52 additions & 0 deletions crates/nix_health/src/check/direnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use semver::Version;

use crate::traits::{Check, CheckResult, Checkable};

use nix_rs::{flake::url::FlakeUrl, info};
Expand Down Expand Up @@ -31,6 +33,13 @@ impl Checkable for Direnv {
return checks;
}

let direnv_version_check = version_check();
let direnv_version_green = direnv_version_check.result.green();
checks.push(direnv_version_check);
if !direnv_version_green {
return checks;
}

let direnv_install_check = install_check(self.required);
let direnv_installed = direnv_install_check.result.green();
checks.push(direnv_install_check);
Expand Down Expand Up @@ -71,6 +80,37 @@ fn install_check(required: bool) -> Check {
}
}

/// [Check] that direnv version >= 2.33.0 for `direnv status --json` support
fn version_check() -> Check {
let suggestion = "Upgrade direnv to >= 2.33.0".to_string();
let direnv_version = direnv_version();
let parsed_direnv_version = direnv_version
.as_ref()
.map(|version| Version::parse(version).ok());
Check {
title: "Direnv version".to_string(),
info: format!("direnv version = {:?}", direnv_version),
// Use semver to compare versions
result: match parsed_direnv_version {
Ok(Some(version)) if version >= Version::parse("2.33.0").unwrap() => CheckResult::Green,
Ok(Some(version)) => CheckResult::Red {
msg: format!("direnv version {} is not supported", version),
suggestion,
},
Ok(None) => CheckResult::Red {
msg: "Unable to parse direnv version".to_string(),
suggestion,
},
Err(e) => CheckResult::Red {
msg: format!("Unable to locate direnv: {}", e),
suggestion,
},
},
required: false,
}
}

/// [Check] that direnv was activated on the local flake
fn activation_check(local_flake: &std::path::Path, required: bool) -> Check {
Expand Down Expand Up @@ -109,6 +149,18 @@ fn is_direnv_allowed_on(project_dir: &std::path::Path) -> anyhow::Result<bool> {
}
}

fn direnv_version() -> anyhow::Result<String> {
let output = std::process::Command::new("direnv")
.args(["--version"])
.output()?;
if output.status.success() {
let out = String::from_utf8_lossy(&output.stdout);
Ok(out.to_string())
} else {
anyhow::bail!("Unable to run direnv --version: {:?}", output.stderr)
}
}

/// Information about the direnv status
#[derive(Debug, Serialize, Deserialize, Clone)]
struct DirenvStatus {
Expand Down

0 comments on commit af3027b

Please sign in to comment.