Skip to content

Commit

Permalink
Extended version (#63)
Browse files Browse the repository at this point in the history
* Swap the --version flag for a new one providing both git hash and build date

* Use timestamp with UTC time

* Cleanup
  • Loading branch information
chevdor authored Apr 13, 2023
1 parent a7eb344 commit 8e2ed46
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
1 change: 1 addition & 0 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 cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ clap = { version = "4.0", features = [
color-eyre = "0.6"
env_logger = "0.10"
log = "0.4"
serde_json = "1.0"
substrate-runtime-proposal-hash = { version = "0.19.0", path = "../libs/substrate-runtime-proposal-hash", optional = true }
subwasmlib = { version = "0.19.0", path = "../lib" }
wasm-loader = { version = "0.19.0", path = "../libs/wasm-loader" }
Expand Down
55 changes: 55 additions & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{borrow::Cow, process::Command};

fn main() {
generate_cargo_keys();
}

/// Generate the cargo keys
pub fn generate_cargo_keys() {
generate_cargo_key_git();
generate_cargo_key_build_date();
}

pub fn generate_cargo_key_build_date() {
let build_date = match Command::new("date").args(["-u", "+%FT%TZ"]).output() {
Ok(o) if o.status.success() => {
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
Cow::from(sha)
}
Ok(o) => {
let status = o.status;
println!("cargo:warning=Failed fetching the date timestamp: {status}");
Cow::from("unknown")
}
Err(err) => {
println!("cargo:warning=Failed fetching the datge: {err}");
Cow::from("unknown")
}
};

println!("cargo:rustc-env=SUBWASM_CLI_BUILD_DATE={build_date}");
}

pub fn generate_cargo_key_git() {
let commit = if let Ok(hash) = std::env::var("SUBWASM_CLI_GIT_COMMIT_HASH") {
Cow::from(hash.trim().to_owned())
} else {
match Command::new("git").args(["rev-parse", "--short=11", "HEAD"]).output() {
Ok(o) if o.status.success() => {
let tmsp = String::from_utf8_lossy(&o.stdout).trim().to_owned();
Cow::from(tmsp)
}
Ok(o) => {
let status = o.status;
println!("cargo:warning=Git command failed with status: {status}");
Cow::from("unknown")
}
Err(err) => {
println!("cargo:warning=Failed to execute git command: {err}");
Cow::from("unknown")
}
}
};

println!("cargo:rustc-env=SUBWASM_CLI_GIT_COMMIT_HASH={commit}");
}
51 changes: 31 additions & 20 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
mod opts;

use std::io::Write;
use std::{env, io::Write};

use clap::{crate_name, crate_version, Parser};
// use color_eyre::owo_colors::OwoColorize;
use env_logger::Env;
use log::info;
use opts::*;
use serde_json::json;
use subwasmlib::*;

/// Simple macro that only execute $statement if $opts don#t contain neither the quiet nor the json flag
macro_rules! noquiet {
( $opts:ident, $statement:expr ) => {{
if !$opts.quiet && !$opts.json {
$statement
}
}};
}

/// Main entry point of the `subwasm` cli
fn main() -> color_eyre::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("none")).init();
let opts: Opts = Opts::parse();
color_eyre::install()?;

noquiet!(opts, println!("Running {} v{}", crate_name!(), crate_version!()));

match opts.subcmd {
SubCommand::Get(get_opts) => {
Some(SubCommand::Get(get_opts)) => {
let chain_name = get_opts.chain.map(|some| some.name);
let url = &get_url(chain_name.as_deref(), &get_opts.url);

download_runtime(url, get_opts.block, get_opts.output)?;
}

SubCommand::Info(info_opts) => {
Some(SubCommand::Info(info_opts)) => {
let chain_name = info_opts.chain.map(|some| some.name);
let source = get_source(chain_name.as_deref(), info_opts.source, info_opts.block);

Expand All @@ -44,7 +33,7 @@ fn main() -> color_eyre::Result<()> {
subwasm.runtime_info().print(opts.json);
}

SubCommand::Version(version_opts) => {
Some(SubCommand::Version(version_opts)) => {
let chain_name = version_opts.chain.map(|some| some.name);
let source = get_source(chain_name.as_deref(), version_opts.source, version_opts.block);

Expand All @@ -54,7 +43,7 @@ fn main() -> color_eyre::Result<()> {
subwasm.runtime_info().print_version(opts.json);
}

SubCommand::Metadata(meta_opts) => {
Some(SubCommand::Metadata(meta_opts)) => {
let chain_name = meta_opts.chain.map(|some| some.name);
let source = get_source(chain_name.as_deref(), meta_opts.source, meta_opts.block);

Expand Down Expand Up @@ -100,7 +89,7 @@ fn main() -> color_eyre::Result<()> {
}?
}

SubCommand::Diff(diff_opts) => {
Some(SubCommand::Diff(diff_opts)) => {
let chain_a = diff_opts.chain_a.map(|some| some.name);
let src_a = get_source(chain_a.as_deref(), diff_opts.src_a, None);

Expand All @@ -110,13 +99,35 @@ fn main() -> color_eyre::Result<()> {
diff(src_a, src_b);
}

SubCommand::Compress(copts) => {
Some(SubCommand::Compress(copts)) => {
compress(copts.input, copts.output)?;
}

SubCommand::Decompress(dopts) => {
Some(SubCommand::Decompress(dopts)) => {
decompress(dopts.input, dopts.output)?;
}

None => {
if opts.version {
let name = crate_name!();
let version = crate_version!();
let commit_hash = env::var("SUBWASM_CLI_GIT_COMMIT_HASH").unwrap_or_else(|_| "n/a".to_string());
let build_date = env::var("SUBWASM_CLI_BUILD_DATE").unwrap_or_else(|_| "n/a".to_string());

if !opts.json {
println!("{name} v{version}-{commit_hash} built {build_date}");
} else {
let version_data = json!({
"name": name,
"version": version,
"commit": commit_hash,
"build_date": build_date,
});
let s = serde_json::to_string_pretty(&version_data).unwrap();
println!("{s}");
}
}
}
};

Ok(())
Expand Down
8 changes: 6 additions & 2 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wasm_loader::{OnchainBlock, Source};

/// `subwasm` allows fetching, parsing and calling some methods on WASM runtimes of Substrate based chains.
#[derive(Parser)]
#[clap(version = crate_version!(), author = crate_authors!(), color=ColorChoice::Auto)]
#[clap(color=ColorChoice::Auto, disable_version_flag = true)]
pub struct Opts {
/// Output as json
#[clap(short, long, global = true)]
Expand All @@ -16,7 +16,11 @@ pub struct Opts {
pub quiet: bool,

#[clap(subcommand)]
pub subcmd: SubCommand,
pub subcmd: Option<SubCommand>,

/// Show the version
#[clap(short, long, alias = "V")]
pub version: bool,
}

/// You can find all available commands below.
Expand Down

0 comments on commit 8e2ed46

Please sign in to comment.