From 2e89addb86d8fb46f908c20067ec4b030306ed9e Mon Sep 17 00:00:00 2001 From: alcroito Date: Mon, 3 Jan 2022 21:38:34 +0200 Subject: [PATCH] Add command line option to print build info Like commit sha1, rustc version used to build the app, host and target triplets, etc. --- .github/workflows/ci.yaml | 8 ++ Cargo.lock | 183 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++ TODO.md | 1 - build.rs | 16 ++++ src/build_info.rs | 62 +++++++++++++ src/cli.rs | 4 + src/config_consts.rs | 1 + src/lib.rs | 1 + src/main.rs | 6 ++ 10 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 build.rs create mode 100644 src/build_info.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f8e1427..a2a0370 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -190,6 +190,14 @@ jobs: command: build args: --release --target=${{ matrix.rust_target_arch }} + - name: Run app with --build-info + uses: actions-rs/cargo@v1 + if: matrix.cross == false + with: + use-cross: ${{ matrix.cross }} + command: run + args: --release --target=${{ matrix.rust_target_arch }} -- --build-info + - name: Create staging archive id: create_staging_archive uses: ./.github/actions/create_archive diff --git a/Cargo.lock b/Cargo.lock index e019bac..b59e56e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,9 @@ name = "cc" version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +dependencies = [ + "jobserver", +] [[package]] name = "cfg-if" @@ -97,6 +100,7 @@ dependencies = [ "num-integer", "num-traits", "serde", + "time", "winapi", ] @@ -157,8 +161,15 @@ dependencies = [ "tempfile", "toml", "trust-dns-resolver", + "vergen", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "encoding_rs" version = "0.8.30" @@ -180,6 +191,26 @@ dependencies = [ "syn", ] +[[package]] +name = "enum-iterator" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eeac5c5edb79e4e39fe8439ef35207780a11f69c52cbe424ce3dfad4cb78de6" +dependencies = [ + "enum-iterator-derive", +] + +[[package]] +name = "enum-iterator-derive" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c134c37760b27a871ba422106eedbb8247da973a09e82558bf26d619c882b159" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "fern" version = "0.6.0" @@ -279,6 +310,31 @@ dependencies = [ "wasi", ] +[[package]] +name = "getset" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e45727250e75cc04ff2846a66397da8ef2b3db8e40e0cef4df67950a07621eb9" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "git2" +version = "0.13.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29229cc1b24c0e6062f6e742aa3e256492a5323365e5ed3413599f8a5eff7d6" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "h2" version = "0.3.9" @@ -470,6 +526,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +[[package]] +name = "jobserver" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.55" @@ -491,6 +556,30 @@ version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +[[package]] +name = "libgit2-sys" +version = "0.12.26+1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e1c899248e606fbfe68dcb31d8b0176ebab833b103824af31bddf4b7457494" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + +[[package]] +name = "libz-sys" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de5435b8549c16d423ed0c03dbaafe57cf6c3344744f1242520d59c9d8ecec66" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.5.4" @@ -742,6 +831,30 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.36" @@ -869,6 +982,21 @@ dependencies = [ "quick-error", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "rustversion" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f" + [[package]] name = "ryu" version = "1.0.9" @@ -923,6 +1051,12 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" + [[package]] name = "serde" version = "1.0.133" @@ -1036,6 +1170,21 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sysinfo" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e7de153d0438a648bb71e06e300e54fc641685e96af96d49b843f43172d341c" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "doc-comment", + "libc", + "ntapi", + "once_cell", + "winapi", +] + [[package]] name = "tempfile" version = "3.2.0" @@ -1079,6 +1228,16 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "tinyvec" version = "1.5.1" @@ -1276,6 +1435,30 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "vergen" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0c9f8387e118573859ae0e6c6fbdfa41bd1f4fbea451b0b8c5a81a3b8bc9e0" +dependencies = [ + "anyhow", + "cfg-if", + "chrono", + "enum-iterator", + "getset", + "git2", + "rustc_version", + "rustversion", + "sysinfo", + "thiserror", +] + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "want" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 83d7271..6093904 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT" keywords = ["dyndns", "digitalocean"] exclude = ["/ci/*", "/.github/*"] publish = false +build = "build.rs" [[bin]] name = "do_ddns" @@ -35,5 +36,9 @@ signal-hook = { version = "0.3", features = ["extended-siginfo"] } toml = "0.5" trust-dns-resolver = "0.20" +[build-dependencies] +anyhow = "1.0" +vergen = "6" + [dev-dependencies] tempfile = "3" diff --git a/TODO.md b/TODO.md index 98fd369..5736a85 100644 --- a/TODO.md +++ b/TODO.md @@ -4,4 +4,3 @@ * Add support for detecting systemd notify socket to remove timestamp from logs * Deduplicate `from_x` code in ConfigValueBuilder * Add systemd sample configuration -* Embed commit SHA1 into help text. diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..5c1fad4 --- /dev/null +++ b/build.rs @@ -0,0 +1,16 @@ +use anyhow::Result; +use vergen::{vergen, Config, SemverKind, TimestampKind}; + +fn main() -> Result<()> { + let mut config = Config::default(); + // Enable build date generation + *config.build_mut().kind_mut() = TimestampKind::All; + + // Change the SEMVER output to the lightweight variant + *config.git_mut().semver_kind_mut() = SemverKind::Lightweight; + // Add a `-dirty` flag to the SEMVER output + *config.git_mut().semver_dirty_mut() = Some("-dirty"); + + // Generate the instructions + vergen(config) +} diff --git a/src/build_info.rs b/src/build_info.rs new file mode 100644 index 0000000..2d22c0f --- /dev/null +++ b/src/build_info.rs @@ -0,0 +1,62 @@ +use crate::config_consts::BUILD_INFO; +use clap::ArgMatches; + +fn print_build_info() { + println!( + " +Build date: {} +Build timestamp: {} +Build version: {} +Commit SHA: {:?} +Commit timestamp: {:?} +Commit branch: {:?} +Commit SemVer: {:?} +Rust channel: {} +Rust commit date: {} +Rust commit SHA: {} +Rust host triple: {} +Rust llvm version: {} +Rust version: {} +Cargo target triple: {} +Cargo profile: {} +Cargo features: {} +Host platform: {} +Host OS: {} +Host memory: {} +Host CPU: {} +Host CPU core count: {} +Host CPU brand: {} +", + env!("VERGEN_BUILD_DATE"), + env!("VERGEN_BUILD_TIMESTAMP"), + env!("VERGEN_BUILD_SEMVER"), + option_env!("VERGEN_GIT_SHA"), + option_env!("VERGEN_GIT_COMMIT_TIMESTAMP"), + option_env!("VERGEN_GIT_BRANCH"), + option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT"), + env!("VERGEN_RUSTC_CHANNEL"), + env!("VERGEN_RUSTC_COMMIT_DATE"), + env!("VERGEN_RUSTC_COMMIT_HASH"), + env!("VERGEN_RUSTC_HOST_TRIPLE"), + env!("VERGEN_RUSTC_LLVM_VERSION"), + env!("VERGEN_RUSTC_SEMVER"), + env!("VERGEN_CARGO_TARGET_TRIPLE"), + env!("VERGEN_CARGO_PROFILE"), + env!("VERGEN_CARGO_FEATURES"), + env!("VERGEN_SYSINFO_NAME"), + env!("VERGEN_SYSINFO_OS_VERSION"), + env!("VERGEN_SYSINFO_TOTAL_MEMORY"), + env!("VERGEN_SYSINFO_CPU_VENDOR"), + env!("VERGEN_SYSINFO_CPU_CORE_COUNT"), + env!("VERGEN_SYSINFO_CPU_BRAND"), + ); +} + +pub fn print_build_info_if_requested(clap_matches: &ArgMatches<'static>) -> bool { + if clap_matches.is_present(BUILD_INFO) { + print_build_info(); + true + } else { + false + } +} diff --git a/src/cli.rs b/src/cli.rs index 02d2e6b..dfb6522 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -167,6 +167,10 @@ Env var: DO_DYNDNS_UPDATE_INTERVAL=2hours 30mins", "\ Show what would have been updated. Env var: DO_DYNDNS_DRY_RUN=true", + )) + .arg(Arg::with_name(BUILD_INFO).long("build-info").help( + "\ +Output build info like git commit sha, rustc version, etc", )) .get_matches(); matches diff --git a/src/config_consts.rs b/src/config_consts.rs index 70040ab..f1d9b9a 100644 --- a/src/config_consts.rs +++ b/src/config_consts.rs @@ -9,5 +9,6 @@ pub static SERVICE_LOG_LEVEL: &str = "log_level"; pub static DRY_RUN: &str = "dry_run"; pub static LOG_LEVEL_VERBOSITY_SHORT: &str = "v"; pub static ENV_VAR_PREFIX: &str = "DO_DYNDNS_"; +pub static BUILD_INFO: &str = "build_info"; pub static DOMAINS_CONFIG_KEY: &str = "domains"; diff --git a/src/lib.rs b/src/lib.rs index 7b64a3e..9d75f72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod build_info; pub mod cli; pub mod config; pub mod config_builder; diff --git a/src/main.rs b/src/main.rs index d29ad05..e30f48f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Result}; +use do_ddns::build_info::print_build_info_if_requested; use do_ddns::cli::get_clap_matches; use do_ddns::config::Config; use do_ddns::config_builder::config_with_args; @@ -20,6 +21,11 @@ fn main() -> Result<()> { fn main_impl() -> Result<()> { let clap_matches = get_clap_matches(); + + if print_build_info_if_requested(&clap_matches) { + return Ok(()); + } + let config = config_with_args(&clap_matches)?; start_daemon(config)?; Ok(())