Skip to content

Commit

Permalink
misc: generate a smarter version string
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyscot committed Nov 1, 2024
1 parent 3692293 commit 4cd506f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anstyle = "1.0.8"
anyhow = "1.0.89"
capnp = "0.20.1"
capnp-futures = "0.20.0"
clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help"] }
clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help", "string"] }
console = "0.15.8"
dns-lookup = "2.0.4"
futures-util = { version = "0.3.31", default-features = false }
Expand Down
16 changes: 12 additions & 4 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// QCP top-level command-line arguments
// (c) 2024 Ross Younger

use crate::build_info;
use clap::Parser;
use clap::{Args as _, FromArgMatches as _, Parser};

/// Options that switch us into another mode i.e. which don't require source/destination arguments
pub(crate) const MODE_OPTIONS: &[&str] = &["server", "help_buffers"];

#[derive(Debug, Parser, Clone)]
#[command(
author,
version,
version(build_info::GIT_VERSION),
// we set short/long version strings explicitly, see custom_parse()
about,
before_help = "e.g. qcp some/file my-server:some-directory/",
infer_long_args(true)
Expand Down Expand Up @@ -70,3 +68,13 @@ pub(crate) struct CliArgs {
//
// N.B. ClientOptions has positional arguments!
}

impl CliArgs {
/// Sets up and executes ou
pub(crate) fn custom_parse() -> Self {
let cli = clap::Command::new(clap::crate_name!());
let ver = crate::version::short();
let cli = CliArgs::augment_args(cli).version(ver);
CliArgs::from_arg_matches(&cli.get_matches_from(std::env::args_os())).unwrap()
}
}
3 changes: 1 addition & 2 deletions src/cli/cli_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
server::server_main,
util::setup_tracing,
};
use clap::Parser;
use indicatif::{MultiProgress, ProgressDrawTarget};
use tracing::error_span;

Expand All @@ -23,7 +22,7 @@ use tracing::error_span;
#[tokio::main(flavor = "current_thread")]
#[allow(clippy::missing_panics_doc)]
pub async fn cli() -> anyhow::Result<ExitCode> {
let args = CliArgs::parse();
let args = CliArgs::custom_parse();
if args.help_buffers {
os::print_udp_buffer_size_help_message(
args.bandwidth.recv_buffer(),
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,4 @@ pub mod doc;

pub mod os;

/// Build-time info (autogenerated by `built`)
#[allow(unreachable_pub)]
mod build_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
mod version;
19 changes: 19 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Autogenerated build-time information
// (c) 2024 Ross Younger

/// Build-time info (autogenerated by `built`)
#[allow(unreachable_pub)]
mod build_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

/// Short version string
///
/// If possible use GIT_VERSION; if not, do what we can
pub(crate) fn short() -> String {
if let Some(ver) = build_info::GIT_VERSION {
return ver.into();
}
let hash = build_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown");
format!("{}-{}", clap::crate_version!(), hash)
}

0 comments on commit 4cd506f

Please sign in to comment.