From e4366a2b7c22ba198b0e797f2dddabe0fd3b6dc3 Mon Sep 17 00:00:00 2001 From: Petard Jonson <41122242+greenhat616@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:20:19 +0800 Subject: [PATCH] feat: better version print --- Cargo.lock | 125 +++++++++++++++++++++++++++++++ Cargo.toml | 7 +- nyanpasu_service/Cargo.toml | 8 ++ nyanpasu_service/build.rs | 61 +++++++++++++++ nyanpasu_service/src/cmds/mod.rs | 71 +++++++++++++++++- nyanpasu_service/src/consts.rs | 10 +++ 6 files changed, 275 insertions(+), 7 deletions(-) create mode 100644 nyanpasu_service/build.rs diff --git a/Cargo.lock b/Cargo.lock index 4cb6739..e9d1024 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.14" @@ -347,6 +362,21 @@ dependencies = [ "windows 0.51.1", ] +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-targets 0.52.6", +] + [[package]] name = "clang-sys" version = "1.8.1" @@ -413,12 +443,28 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "constcat" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f2e5af989b1955b092db01462980c0a286217f86817e12b2c09aea46bd03651" +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "cpufeatures" version = "0.2.12" @@ -991,6 +1037,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core 0.52.0", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -1038,6 +1107,15 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "isolang" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe50d48c77760c55188549098b9a7f6e37ae980c586a24693d6b01c3b2010c3c" +dependencies = [ + "phf", +] + [[package]] name = "itertools" version = "0.12.1" @@ -1368,18 +1446,22 @@ dependencies = [ "axum", "bounded-vec-deque", "check_elevation", + "chrono", "clap", + "colored", "constcat", "ctrlc", "kill_tree", "nyanpasu-ipc", "nyanpasu-utils", "parking_lot", + "rustc_version", "semver", "serde", "service-manager", "simd-json", "thiserror", + "timeago", "tokio", "tracing", "tracing-appender", @@ -1483,6 +1565,24 @@ dependencies = [ "indexmap", ] +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -1718,6 +1818,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.38.34" @@ -1889,6 +1998,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "slab" version = "0.4.9" @@ -2020,6 +2135,16 @@ dependencies = [ "time-core", ] +[[package]] +name = "timeago" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1710e589de0a76aaf295cd47a6699f6405737dbfd3cf2b75c92d000b548d0e6" +dependencies = [ + "chrono", + "isolang", +] + [[package]] name = "tinyvec" version = "1.8.0" diff --git a/Cargo.toml b/Cargo.toml index c05de46..c95306a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,13 +10,9 @@ repository = "https://github.com/LibNyanpasu/clash-nyanpasu-service.git" [workspace.dependencies] nyanpasu-utils = { git = "https://github.com/LibNyanpasu/nyanpasu-utils.git", default-features = false } -warp = "0.3" axum = "0.7.4" anyhow = "1.0" thiserror = "1" -log = "0.4.20" -log4rs = "1.2.0" -serde_json = "1.0" parking_lot = "0.12.1" simd-json = "0.13" tokio = { version = "1", features = ["full"] } @@ -39,10 +35,9 @@ opentelemetry_sdk = "0.23" opentelemetry-stdout = { version = "0.4", features = ["trace"] } tracing-opentelemetry = "0.24" console-subscriber = "0.3.0" -rfd = "0.14.0" futures = "0.3.30" futures-util = "0.3" -runas = "1.2" +chrono = { version = "0.4", features = ["serde"] } [profile.release] panic = "unwind" diff --git a/nyanpasu_service/Cargo.toml b/nyanpasu_service/Cargo.toml index 629d28d..dd07412 100644 --- a/nyanpasu_service/Cargo.toml +++ b/nyanpasu_service/Cargo.toml @@ -6,6 +6,7 @@ authors = { workspace = true } license = { workspace = true } repository = { workspace = true } default-run = "nyanpasu-service" +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] @@ -43,6 +44,13 @@ ctrlc = { version = "3", features = ["termination"] } kill_tree = { version = "0.2.4", features = ["tokio"] } semver = "1" bounded-vec-deque = "0.1.1" +chrono = { workspace = true } +colored = "2.1.0" +timeago = "0.4" + +[build-dependencies] +chrono = { workspace = true } +rustc_version = "0.4" [target.'cfg(windows)'.dependencies] check_elevation = "0.2.4" diff --git a/nyanpasu_service/build.rs b/nyanpasu_service/build.rs new file mode 100644 index 0000000..ee7dd3d --- /dev/null +++ b/nyanpasu_service/build.rs @@ -0,0 +1,61 @@ +use chrono::prelude::*; +use rustc_version::version_meta; +use std::{env, process::Command}; + +fn main() { + // Git Information + let output = Command::new("git") + .args([ + "show", + "--pretty=format:'%H,%cn,%cI'", + "--no-patch", + "--no-notes", + ]) + .output() + .unwrap(); + let command_args: Vec = String::from_utf8(output.stdout) + .unwrap() + .replace('\'', "") + .split(',') + .map(String::from) + .collect(); + println!("cargo:rustc-env=COMMIT_HASH={}", command_args[0]); + println!("cargo:rustc-env=COMMIT_AUTHOR={}", command_args[1]); + let commit_date = DateTime::parse_from_rfc3339(command_args[2].as_str()) + .unwrap() + .with_timezone(&Utc) + .to_rfc3339_opts(SecondsFormat::Millis, true); + println!("cargo:rustc-env=COMMIT_DATE={}", commit_date); + + // Build Date + let build_date = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true); + println!("cargo:rustc-env=BUILD_DATE={}", build_date); + + // Build Profile + println!( + "cargo:rustc-env=BUILD_PROFILE={}", + match env::var("PROFILE").unwrap().as_str() { + "release" => "Release", + "debug" => "Debug", + _ => "Unknown", + } + ); + // Build Platform + println!( + "cargo:rustc-env=BUILD_PLATFORM={}", + env::var("TARGET").unwrap() + ); + // Rustc Version & LLVM Version + let rustc_version = version_meta().unwrap(); + println!( + "cargo:rustc-env=RUSTC_VERSION={}", + rustc_version.short_version_string + ); + println!( + "cargo:rustc-env=LLVM_VERSION={}", + match rustc_version.llvm_version { + Some(v) => v.to_string(), + None => "Unknown".to_string(), + } + ) +} diff --git a/nyanpasu_service/src/cmds/mod.rs b/nyanpasu_service/src/cmds/mod.rs index 873c51b..36cc32a 100644 --- a/nyanpasu_service/src/cmds/mod.rs +++ b/nyanpasu_service/src/cmds/mod.rs @@ -13,11 +13,16 @@ mod uninstall; mod update; #[derive(Parser)] -#[command(version, about, long_about = None)] +#[command(version, author, about, long_about = None, disable_version_flag = true)] struct Cli { /// Enable verbose logging #[clap(long, default_value = "false")] verbose: bool, + + /// Print the version + #[clap(short, long, default_value = "false")] + version: bool, + #[command(subcommand)] command: Option, } @@ -73,6 +78,10 @@ pub enum CommandError { pub async fn process() -> Result<(), CommandError> { let cli = Cli::parse(); + if cli.version { + print_version(); + } + if !matches!( cli.command, Some(Commands::Status(_)) | Some(Commands::Rpc(_)) | None @@ -113,3 +122,63 @@ pub async fn process() -> Result<(), CommandError> { } } } + +pub fn print_version() { + use crate::consts::*; + use chrono::{DateTime, Utc}; + use colored::*; + use timeago::Formatter; + + let now = Utc::now(); + let formatter = Formatter::new(); + println!( + "{} {} ({} Build)\n", // + APP_NAME, + format!("v{}", APP_VERSION).yellow(), + BUILD_PROFILE.yellow() + ); + println!("{}", "[Build Information]".bright_black()); + println!( + "{:>14}: {:<15}", + "Commit Info", + format!("{} by {}", COMMIT_HASH.green(), COMMIT_AUTHOR.blue()) + ); + println!( + "{:>14}: {:<15}", + "Commit Time", + format!( + "{} ({})", + formatter + .convert_chrono(DateTime::parse_from_rfc3339(COMMIT_DATE).unwrap(), now) + .red(), + COMMIT_DATE.cyan() + ) + ); + println!( + "{:>14}: {:<15}", + "Build Time", + format!( + "{} ({})", + formatter + .convert_chrono(DateTime::parse_from_rfc3339(BUILD_DATE).unwrap(), now) + .red(), + BUILD_DATE.cyan() + ) + ); + println!( + "{:>14}: {:<15}", + "Build Target", + BUILD_PLATFORM.bright_yellow() + ); + println!( + "{:>14}: {:<15}", + "Rust Version", + RUSTC_VERSION.bright_yellow() + ); + println!( + "{:>14}: {:<15}", + "LLVM Version", + LLVM_VERSION.bright_yellow() + ); + std::process::exit(0); +} diff --git a/nyanpasu_service/src/consts.rs b/nyanpasu_service/src/consts.rs index 7a7ee0e..2b22c13 100644 --- a/nyanpasu_service/src/consts.rs +++ b/nyanpasu_service/src/consts.rs @@ -4,6 +4,16 @@ pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const APP_NAME: &str = env!("CARGO_PKG_NAME"); pub const SERVICE_LABEL: &str = concat!("moe.elaina.", APP_NAME); +// Build info +pub const COMMIT_HASH: &str = env!("COMMIT_HASH"); +pub const COMMIT_AUTHOR: &str = env!("COMMIT_AUTHOR"); +pub const COMMIT_DATE: &str = env!("COMMIT_DATE"); +pub const BUILD_DATE: &str = env!("BUILD_DATE"); +pub const BUILD_PROFILE: &str = env!("BUILD_PROFILE"); +pub const BUILD_PLATFORM: &str = env!("BUILD_PLATFORM"); +pub const RUSTC_VERSION: &str = env!("RUSTC_VERSION"); +pub const LLVM_VERSION: &str = env!("LLVM_VERSION"); + pub enum ExitCode { PermissionDenied = 64, ServiceNotInstalled = 100,