From 957bfddd3bc0df3a3c03b38ba1876ad05bd54041 Mon Sep 17 00:00:00 2001 From: fzdwx Date: Sat, 4 May 2024 20:57:05 +0800 Subject: [PATCH] feat: `ya` add git hash to `-V` --- Cargo.lock | 1 + yazi-cli/Cargo.toml | 1 + yazi-cli/build.rs | 3 +++ yazi-cli/src/args.rs | 9 ++++++--- yazi-cli/src/main.rs | 22 +++++++++++++++++++--- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe2d1f46..e34c9328 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2737,6 +2737,7 @@ dependencies = [ "clap_complete_nushell", "serde_json", "tokio", + "vergen", "yazi-dds", ] diff --git a/yazi-cli/Cargo.toml b/yazi-cli/Cargo.toml index 6464852f..3846844f 100644 --- a/yazi-cli/Cargo.toml +++ b/yazi-cli/Cargo.toml @@ -24,6 +24,7 @@ clap_complete = "4.5.2" clap_complete_fig = "4.5.0" clap_complete_nushell = "4.5.1" serde_json = "1.0.116" +vergen = { version = "8.3.1", features = [ "build", "git", "gitcl" ] } [[bin]] name = "ya" diff --git a/yazi-cli/build.rs b/yazi-cli/build.rs index f01b5d0f..b9eb8efd 100644 --- a/yazi-cli/build.rs +++ b/yazi-cli/build.rs @@ -5,8 +5,11 @@ use std::{env, error::Error}; use clap::CommandFactory; use clap_complete::{generate_to, Shell}; +use vergen::EmitBuilder; fn main() -> Result<(), Box> { + EmitBuilder::builder().build_date().git_sha(true).emit()?; + if env::var_os("YAZI_GEN_COMPLETIONS").is_none() { return Ok(()); } diff --git a/yazi-cli/src/args.rs b/yazi-cli/src/args.rs index 554f2a1b..7e6672c6 100644 --- a/yazi-cli/src/args.rs +++ b/yazi-cli/src/args.rs @@ -4,11 +4,14 @@ use anyhow::{bail, Result}; use clap::{command, Parser, Subcommand}; #[derive(Parser)] -#[command(name = "ya", version, about, long_about = None)] -#[command(propagate_version = true)] +#[command(name = "ya")] pub(super) struct Args { #[command(subcommand)] - pub(super) command: Command, + pub(super) command: Option, + + /// Print version + #[arg(short = 'V', long)] + pub version: bool, } #[derive(Subcommand)] diff --git a/yazi-cli/src/main.rs b/yazi-cli/src/main.rs index b4ab6b03..0953fb63 100644 --- a/yazi-cli/src/main.rs +++ b/yazi-cli/src/main.rs @@ -1,5 +1,7 @@ mod args; +use std::process; + use args::*; use clap::Parser; @@ -7,22 +9,36 @@ use clap::Parser; async fn main() -> anyhow::Result<()> { let args = Args::parse(); - match &args.command { + if args.version { + println!("Ya {}", action_version()); + process::exit(0); + } + + match &args.command.expect("No command provided") { Command::Pub(cmd) => { yazi_dds::init(); if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver, None, &cmd.body()?).await { eprintln!("Cannot send message: {e}"); - std::process::exit(1); + process::exit(1); } } Command::PubStatic(cmd) => { yazi_dds::init(); if let Err(e) = yazi_dds::Client::shot(&cmd.kind, 0, Some(cmd.severity), &cmd.body()?).await { eprintln!("Cannot send message: {e}"); - std::process::exit(1); + process::exit(1); } } } Ok(()) } + +fn action_version() -> String { + format!( + "{} ({} {})", + env!("CARGO_PKG_VERSION"), + env!("VERGEN_GIT_SHA"), + env!("VERGEN_BUILD_DATE") + ) +}