use crate::built_info;
use crate::cli::args::{OutputFormat, VersionArgs};
pub fn run(args: &VersionArgs, quiet: bool) {
if quiet {
return;
}
let name = env!("CARGO_PKG_NAME");
let version = env!("CARGO_PKG_VERSION");
let git_hash = built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown");
let git_dirty = built_info::GIT_DIRTY.unwrap_or(false);
let build_time = built_info::BUILT_TIME_UTC;
let rustc_version = built_info::RUSTC_VERSION;
let target = built_info::TARGET;
match args.format {
OutputFormat::Human => {
println!("{name} {version}");
if git_dirty {
println!(" commit: {git_hash} (dirty)");
} else {
println!(" commit: {git_hash}");
}
println!(" built: {build_time}");
println!(" rustc: {rustc_version}");
println!(" target: {target}");
}
OutputFormat::Json => {
let json = serde_json::json!({
"name": name,
"version": version,
"commit": git_hash,
"dirty": git_dirty,
"built": build_time,
"rustc": rustc_version,
"target": target,
});
println!(
"{}",
serde_json::to_string_pretty(&json).unwrap_or_else(|_| json.to_string())
);
}
}
}