feat(router): include git commit hash and timestamp in --version output (#29)

This commit is contained in:
Sanchith Hegde
2022-11-28 10:19:57 +05:30
committed by GitHub
parent 3e0298032b
commit cc473590cb
8 changed files with 96 additions and 69 deletions

View File

@ -7,6 +7,7 @@ default-run = "router"
rust-version = "1.63" rust-version = "1.63"
readme = "README.md" readme = "README.md"
license = "Apache-2.0" license = "Apache-2.0"
build = "src/build.rs"
[features] [features]
default = [] default = []
@ -73,6 +74,9 @@ time = { version = "0.3.14", features = ["macros"] }
tokio = "1.21.2" tokio = "1.21.2"
toml = "0.5.9" toml = "0.5.9"
[build-dependencies]
router_env = { version = "0.1.0", path = "../router_env", default-features = false, features = ["vergen"] }
[[bin]] [[bin]]
name = "router" name = "router"
path = "src/bin/router.rs" path = "src/bin/router.rs"

View File

@ -0,0 +1,3 @@
fn main() {
router_env::vergen::generate_cargo_instructions();
}

View File

@ -11,6 +11,7 @@ use crate::{
}; };
#[derive(StructOpt, Default)] #[derive(StructOpt, Default)]
#[structopt(version = router_env::version!())]
pub struct CmdLineConf { pub struct CmdLineConf {
/// Config file. /// Config file.
/// Application will look for "config/config.toml" if this option isn't specified. /// Application will look for "config/config.toml" if this option isn't specified.

View File

@ -27,12 +27,13 @@ tracing-appender = "0.2.2"
tracing-core = "0.1.29" tracing-core = "0.1.29"
tracing-opentelemetry = { version = "0.17" } tracing-opentelemetry = { version = "0.17" }
tracing-subscriber = { version = "0.3.15", default-features = true, features = ["json", "env-filter", "registry"] } tracing-subscriber = { version = "0.3.15", default-features = true, features = ["json", "env-filter", "registry"] }
vergen = { version = "7.4.2", optional = true }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
[build-dependencies] [build-dependencies]
vergen = { version = "7.4.2" } vergen = "7.4.2"
[features] [features]
default = ["actix_web"] default = ["actix_web"]

View File

@ -1,50 +1,5 @@
use vergen::{vergen, Config, ShaKind}; include!("vergen.rs");
fn main() { fn main() {
let mut config = Config::default(); generate_cargo_instructions();
let build = config.build_mut();
*build.enabled_mut() = false;
*build.skip_if_error_mut() = true;
let cargo = config.cargo_mut();
*cargo.enabled_mut() = true;
*cargo.features_mut() = false;
*cargo.profile_mut() = true;
*cargo.target_triple_mut() = true;
let git = config.git_mut();
*git.enabled_mut() = true;
*git.commit_author_mut() = false;
*git.commit_count_mut() = false;
*git.commit_message_mut() = false;
*git.commit_timestamp_mut() = true;
*git.semver_mut() = false;
*git.skip_if_error_mut() = true;
*git.sha_kind_mut() = ShaKind::Both;
*git.skip_if_error_mut() = true;
let rustc = config.rustc_mut();
*rustc.enabled_mut() = true;
*rustc.channel_mut() = false;
*rustc.commit_date_mut() = false;
*rustc.host_triple_mut() = false;
*rustc.llvm_version_mut() = false;
*rustc.semver_mut() = true;
*rustc.sha_mut() = true; // required for sever been available
*rustc.skip_if_error_mut() = true;
let sysinfo = config.sysinfo_mut();
*sysinfo.enabled_mut() = false;
*sysinfo.os_version_mut() = false;
*sysinfo.user_mut() = false;
*sysinfo.memory_mut() = false;
*sysinfo.cpu_vendor_mut() = false;
*sysinfo.cpu_core_count_mut() = false;
*sysinfo.cpu_name_mut() = false;
*sysinfo.cpu_brand_mut() = false;
*sysinfo.cpu_frequency_mut() = false;
*sysinfo.skip_if_error_mut() = true;
vergen(config).expect("Problem determining current platform characteristics");
} }

View File

@ -73,23 +73,25 @@ pub fn workspace_path() -> PathBuf {
} }
} }
/// Version defined in the crate file. /// Version of the crate containing the following information:
/// ///
/// Example: `0.1.0`. /// - Semantic Version from the latest git tag. If tags are not present in the repository, crate
/// version from the crate manifest is used instead.
/// - Short hash of the latest git commit.
/// - Timestamp of the latest git commit.
/// ///
/// Example: `0.1.0-abcd012-2038-01-19T03:14:08Z`.
#[macro_export] #[macro_export]
macro_rules! version { macro_rules! version {
( () => {
) => {{
concat!( concat!(
env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SEMVER"),
"-", "-",
env!("VERGEN_GIT_SHA_SHORT"), env!("VERGEN_GIT_SHA_SHORT"),
"-", "-",
env!("VERGEN_GIT_COMMIT_TIMESTAMP"), env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
) )
}}; };
} }
/// ///
@ -107,8 +109,7 @@ macro_rules! version {
#[macro_export] #[macro_export]
macro_rules! build { macro_rules! build {
( () => {
) => {{
concat!( concat!(
env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_VERSION"),
"-", "-",
@ -122,7 +123,7 @@ macro_rules! build {
"-", "-",
env!("VERGEN_CARGO_TARGET_TRIPLE"), env!("VERGEN_CARGO_TARGET_TRIPLE"),
) )
}}; };
} }
/// ///
@ -133,10 +134,9 @@ macro_rules! build {
#[macro_export] #[macro_export]
macro_rules! commit { macro_rules! commit {
( () => {
) => {{
env!("VERGEN_GIT_SHA") env!("VERGEN_GIT_SHA")
}}; };
} }
// /// // ///
@ -166,10 +166,9 @@ macro_rules! commit {
#[macro_export] #[macro_export]
macro_rules! service_name { macro_rules! service_name {
( () => {
) => {{
env!("CARGO_CRATE_NAME") env!("CARGO_CRATE_NAME")
}}; };
} }
/// ///
@ -180,8 +179,7 @@ macro_rules! service_name {
#[macro_export] #[macro_export]
macro_rules! profile { macro_rules! profile {
( () => {
) => {
env!("VERGEN_CARGO_PROFILE") env!("VERGEN_CARGO_PROFILE")
}; };
} }

View File

@ -19,10 +19,12 @@
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))] #![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))]
pub mod env; pub mod env;
#[doc(inline)]
pub use env::*;
pub mod logger; pub mod logger;
/// `cargo` build instructions generation for obtaining information about the application
/// environment.
#[cfg(feature = "vergen")]
pub mod vergen;
// pub use literally; // pub use literally;
#[doc(inline)] #[doc(inline)]
pub use logger::*; pub use logger::*;
@ -31,3 +33,6 @@ pub use tracing;
#[cfg(feature = "actix_web")] #[cfg(feature = "actix_web")]
pub use tracing_actix_web; pub use tracing_actix_web;
pub use tracing_appender; pub use tracing_appender;
#[doc(inline)]
pub use self::env::*;

View File

@ -0,0 +1,60 @@
/// Configures the [`vergen`][::vergen] crate to generate the `cargo` build instructions.
///
/// This function should be typically called within build scripts to generate `cargo` build
/// instructions for the corresponding crate.
///
/// # Panics
///
/// Panics if `vergen` fails to generate `cargo` build instructions.
#[allow(clippy::expect_used)]
pub fn generate_cargo_instructions() {
use vergen::{vergen, Config, ShaKind};
let mut config = Config::default();
let build = config.build_mut();
*build.enabled_mut() = false;
*build.skip_if_error_mut() = true;
let cargo = config.cargo_mut();
*cargo.enabled_mut() = true;
*cargo.features_mut() = false;
*cargo.profile_mut() = true;
*cargo.target_triple_mut() = true;
let git = config.git_mut();
*git.enabled_mut() = true;
*git.commit_author_mut() = false;
*git.commit_count_mut() = false;
*git.commit_message_mut() = false;
*git.commit_timestamp_mut() = true;
*git.semver_mut() = true;
*git.semver_dirty_mut() = Some("-dirty");
*git.skip_if_error_mut() = true;
*git.sha_kind_mut() = ShaKind::Both;
*git.skip_if_error_mut() = true;
let rustc = config.rustc_mut();
*rustc.enabled_mut() = true;
*rustc.channel_mut() = false;
*rustc.commit_date_mut() = false;
*rustc.host_triple_mut() = false;
*rustc.llvm_version_mut() = false;
*rustc.semver_mut() = true;
*rustc.sha_mut() = true; // required for semver to be available
*rustc.skip_if_error_mut() = true;
let sysinfo = config.sysinfo_mut();
*sysinfo.enabled_mut() = false;
*sysinfo.os_version_mut() = false;
*sysinfo.user_mut() = false;
*sysinfo.memory_mut() = false;
*sysinfo.cpu_vendor_mut() = false;
*sysinfo.cpu_core_count_mut() = false;
*sysinfo.cpu_name_mut() = false;
*sysinfo.cpu_brand_mut() = false;
*sysinfo.cpu_frequency_mut() = false;
*sysinfo.skip_if_error_mut() = true;
vergen(config).expect("Failed to generate `cargo` build instructions");
}