Skip to content

Commit

Permalink
Merge pull request #3454 from ljedrz/feat/version_option2
Browse files Browse the repository at this point in the history
[Feature] Add a --version cli option
  • Loading branch information
zosorock authored Dec 10, 2024
2 parents d7b8c16 + 0911d45 commit 0e95895
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ tikv-jemallocator = "0.5"
[dev-dependencies.rusty-hook]
version = "0.11.2"

[build-dependencies.built]
version = "0.7"
features = [ "git2" ]

[build-dependencies.walkdir]
version = "2"

Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ fn check_file_licenses<P: AsRef<Path>>(path: P) {
fn main() {
// Check licenses in the current folder.
check_file_licenses(".");
// Register build-time information.
built::write_built_file().expect("Failed to acquire build-time information");
}
3 changes: 2 additions & 1 deletion cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ const STYLES: Styles = Styles::plain()
.usage(Style::new().bold().fg_color(HEADER_COLOR))
.literal(Style::new().bold().fg_color(LITERAL_COLOR));

// Note: the basic clap-supplied version is overridden in the main module.
#[derive(Debug, Parser)]
#[clap(name = "snarkOS", author = "The Aleo Team <[email protected]>", styles = STYLES)]
#[clap(name = "snarkOS", author = "The Aleo Team <[email protected]>", styles = STYLES, version)]
pub struct CLI {
/// Specify the verbosity [options: 0, 1, 2, 3]
#[clap(default_value = "2", short, long)]
Expand Down
25 changes: 24 additions & 1 deletion snarkos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use snarkos_cli::{commands::CLI, helpers::Updater};

use clap::Parser;
use std::process::exit;
use std::{env, process::exit};

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
use tikv_jemallocator::Jemalloc;
Expand All @@ -25,7 +25,13 @@ use tikv_jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

// Obtain information on the build.
include!(concat!(env!("OUT_DIR"), "/built.rs"));

fn main() -> anyhow::Result<()> {
// A hack to avoid having to go through clap to display advanced version information.
check_for_version();

// Parse the given arguments.
let cli = CLI::parse();
// Run the updater.
Expand All @@ -40,3 +46,20 @@ fn main() -> anyhow::Result<()> {
}
Ok(())
}

/// Checks whether the version information was requested and - if so - display it and exit.
fn check_for_version() {
if let Some(first_arg) = env::args().nth(1) {
if ["--version", "-V"].contains(&&*first_arg) {
let version = PKG_VERSION;
let branch = GIT_HEAD_REF.unwrap_or("unknown_branch");
let commit = GIT_COMMIT_HASH.unwrap_or("unknown_commit");
let mut features = FEATURES_LOWERCASE_STR.to_owned();
features.retain(|c| c != ' ');

println!("snarkos {version} {branch} {commit} features=[{features}]");

exit(0);
}
}
}

0 comments on commit 0e95895

Please sign in to comment.