Skip to content

Commit 360208c

Browse files
committed
cli: include build commit in version output
Signed-off-by: Gabriel Bercaru <[email protected]>
1 parent d138ae1 commit 360208c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

build.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
// Get latest commit SHA.
5+
let output = Command::new("git")
6+
.arg("describe")
7+
.arg("--always")
8+
.arg("--dirty")
9+
.output();
10+
11+
// Convert command output to latest commit SHA and set an environment
12+
// variable ("COMMIT_ID", only available in the build context) to
13+
// the aforementioned commit SHA.
14+
let stdout;
15+
let output_str: &str = match output {
16+
Ok(output) => {
17+
stdout = output.stdout;
18+
std::str::from_utf8(&stdout).expect("Invalid UTF-8 string provided")
19+
}
20+
_ => "",
21+
};
22+
23+
println!("cargo:rustc-env=COMMIT_ID={}", output_str.trim());
24+
}

src/main.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ use nitro_cli::{build_enclaves, console_enclaves, create_app};
2323

2424
/// *Nitro CLI* application entry point.
2525
fn main() {
26+
// Custom version (possibly including build commit).
27+
let commit_id = env!("COMMIT_ID");
28+
let version_str: String = match commit_id.len() {
29+
0 => env!("CARGO_PKG_VERSION").to_string(),
30+
_ => format!(
31+
"{} (build commit: {})",
32+
env!("CARGO_PKG_VERSION"),
33+
commit_id
34+
),
35+
};
36+
2637
// Command-line specification for the Nitro CLI.
27-
let app = create_app!();
38+
let mut app = create_app!();
39+
app = app.version(&*version_str);
2840
let args = app.get_matches();
2941
let logger = logger::init_logger();
3042
let mut replies: Vec<UnixStream> = vec![];

0 commit comments

Comments
 (0)