diff --git a/CLAUDE.md b/CLAUDE.md index 9b52a4f0..b7f0304d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -47,3 +47,7 @@ cargo clippy --all-targets -- -D warnings When the user asks to run clippy and provides the ability to run on both targets, try to run it on both targets. + +## Formatting + +After modifying code, run `cargo fmt` to ensure consistent formatting before committing changes. diff --git a/README.md b/README.md index ad537437..6fde69e9 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ OPTIONS: --timeout Timeout for command execution -v, --verbose Increase verbosity (-vvv for max) -h, --help Print help - -V, --version Print version + -V, --version Print version with commit hash RULE FORMAT: Rules are specified with -r/--rule and use the format: diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..dcd7eb46 --- /dev/null +++ b/build.rs @@ -0,0 +1,28 @@ +use std::env; +use std::process::Command; + +fn main() { + // Retrieve crate version provided by Cargo + let version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string()); + + // Try to get short commit hash + let git_hash = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output() + .ok() + .and_then(|o| { + if o.status.success() { + Some(String::from_utf8_lossy(&o.stdout).trim().to_string()) + } else { + None + } + }) + .unwrap_or_else(|| "unknown".to_string()); + + // Export as environment variables for use in the code and tests + println!("cargo:rustc-env=GIT_HASH={}", git_hash); + println!( + "cargo:rustc-env=VERSION_WITH_GIT_HASH={} ({})", + version, git_hash + ); +} diff --git a/src/main.rs b/src/main.rs index ced0a4f5..e5cdab07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use tracing::{debug, info, warn}; #[derive(Parser, Debug)] #[command(name = "httpjail")] -#[command(version, about, long_about = None)] +#[command(version = env!("VERSION_WITH_GIT_HASH"), about, long_about = None)] #[command(about = "Monitor and restrict HTTP/HTTPS requests from processes")] struct Args { /// Rules for filtering requests (can be specified multiple times) diff --git a/tests/smoke_test.rs b/tests/smoke_test.rs index 2a2d5ca9..2c3d8a69 100644 --- a/tests/smoke_test.rs +++ b/tests/smoke_test.rs @@ -15,10 +15,10 @@ fn test_httpjail_help() { fn test_httpjail_version() { let mut cmd = Command::cargo_bin("httpjail").unwrap(); cmd.arg("--version"); - + let hash = env!("GIT_HASH"); cmd.assert() .success() - .stdout(predicate::str::contains("httpjail")); + .stdout(predicate::str::contains("httpjail").and(predicate::str::contains(hash))); } #[test]