prdoclib/commands/
version.rs

1//! Implementation of the version command
2
3use serde_json::json;
4
5/// Wrapper for the version command
6pub struct VersionCmd;
7
8impl VersionCmd {
9	/// Run the version command
10	pub fn run(name: &str, version: &str, json: bool) {
11		let commit_hash = std::env::var("PRDOC_CLI_GIT_COMMIT_HASH");
12		let build_date = std::env::var("PRDOC_CLI_BUILD_DATE");
13
14		if !json {
15			let commit_hash_str =
16				if let Ok(s) = commit_hash { format!("-{s}") } else { String::from("") };
17			let build_date_str =
18				if let Ok(s) = build_date { format!(" built {s}") } else { String::from("") };
19			println!("{name} v{version}{commit_hash_str}{build_date_str}");
20		} else {
21			let version_data = json!({
22				"name": name,
23				"version": version,
24				"commit": commit_hash.unwrap_or_default(),
25				"build_date": build_date.unwrap_or_default(),
26			});
27			let s =
28				serde_json::to_string_pretty(&version_data).expect("serde_json ran into issues");
29			println!("{s}");
30		}
31	}
32}