Skip to content

Commit

Permalink
feat: display detailed version for bin instead of the default for eas…
Browse files Browse the repository at this point in the history
…ier debugging (dragonflyoss#754)

Signed-off-by: cormick <[email protected]>
  • Loading branch information
CormickKneey authored Sep 30, 2024
1 parent 9d1ad9c commit a9ea307
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 10 deletions.
117 changes: 115 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dragonfly-client-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ repository.workspace = true
keywords.workspace = true
license.workspace = true
edition.workspace = true
build = "build.rs"

[dependencies]
dragonfly-client-core.workspace = true
clap.workspace = true
regex.workspace = true
serde.workspace = true
tracing.workspace = true
Expand All @@ -25,3 +27,7 @@ local-ip-address = "0.6.3"
hostname = "^0.4"
humantime-serde = "1.1.1"
serde_regex = "1.1.0"
shadow-rs = "0.35.0"

[build-dependencies]
shadow-rs = "0.35.0"
19 changes: 19 additions & 0 deletions dragonfly-client-config/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

fn main() -> shadow_rs::SdResult<()> {
shadow_rs::new()
}
30 changes: 30 additions & 0 deletions dragonfly-client-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

use clap::{Arg, Command};
use shadow_rs::shadow;
use std::path::PathBuf;

shadow!(build);

pub mod dfcache;
pub mod dfdaemon;
pub mod dfget;
Expand Down Expand Up @@ -99,3 +103,29 @@ pub fn default_cache_dir() -> PathBuf {
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("cache");
}

#[derive(Debug, Clone)]
pub struct DetailedVersionParser;

impl clap::builder::TypedValueParser for DetailedVersionParser {
type Value = bool;
fn parse_ref(
&self,
cmd: &Command,
_arg: Option<&Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
if value.to_os_string().eq("true") {
println!(
"{} {} (@: {}, rust: {}, git: {})",
cmd.get_name(),
cmd.get_version().unwrap_or("unknown"),
&build::COMMIT_DATE[..10],
build::RUST_VERSION,
build::SHORT_COMMIT
);
std::process::exit(0);
}
Ok(false)
}
}
14 changes: 13 additions & 1 deletion dragonfly-client-init/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use clap::Parser;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_config::dfinit;
use dragonfly_client_config::DetailedVersionParser;
use dragonfly_client_init::container_runtime;
use std::path::PathBuf;
use tracing::{error, Level};
Expand All @@ -29,7 +30,8 @@ use tracing::{error, Level};
about = "dfinit is a command line for initializing runtime environment of the dfdaemon",
long_about = "A command line for initializing runtime environment of the dfdaemon, \
For example, if the container's runtime is containerd, then dfinit will modify the mirror configuration of containerd and restart the containerd service. \
It also supports to change configuration of the other container's runtime, such as cri-o, docker, etc."
It also supports to change configuration of the other container's runtime, such as cri-o, docker, etc.",
disable_version_flag = true
)]
struct Args {
#[arg(
Expand Down Expand Up @@ -68,6 +70,16 @@ struct Args {
help = "Specify whether to print log"
)]
verbose: bool,

#[arg(
short = 'V',
long = "version",
help = "Print version",
default_value_t = false,
action = clap::ArgAction::SetTrue,
value_parser = DetailedVersionParser
)]
version: bool,
}

#[tokio::main]
Expand Down
14 changes: 13 additions & 1 deletion dragonfly-client/src/bin/dfcache/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use clap::{Parser, Subcommand};
use dragonfly_client::grpc::dfdaemon_download::DfdaemonDownloadClient;
use dragonfly_client::grpc::health::HealthClient;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_config::DetailedVersionParser;
use dragonfly_client_config::{dfcache, dfdaemon};
use dragonfly_client_core::Result;
use std::path::{Path, PathBuf};
Expand All @@ -35,7 +36,8 @@ pub mod stat;
version,
about = "dfcache is a cache command line based on P2P technology in Dragonfly.",
long_about = "A cache command line based on P2P technology in Dragonfly that can import file and export file in P2P network, \
and it can copy multiple replicas during import. P2P cache is effectively used for fast read and write cache."
and it can copy multiple replicas during import. P2P cache is effectively used for fast read and write cache.",
disable_version_flag = true
)]
struct Args {
#[arg(
Expand Down Expand Up @@ -75,6 +77,16 @@ struct Args {
)]
verbose: bool,

#[arg(
short = 'V',
long = "version",
help = "Print version",
default_value_t = false,
action = clap::ArgAction::SetTrue,
value_parser = DetailedVersionParser
)]
version: bool,

#[command(subcommand)]
command: Command,
}
Expand Down
14 changes: 13 additions & 1 deletion dragonfly-client/src/bin/dfdaemon/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use dragonfly_client::stats::Stats;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_backend::BackendFactory;
use dragonfly_client_config::dfdaemon;
use dragonfly_client_config::DetailedVersionParser;
use dragonfly_client_storage::Storage;
use dragonfly_client_util::id_generator::IDGenerator;
use std::net::SocketAddr;
Expand All @@ -55,7 +56,8 @@ pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0
about = "dfdaemon is a high performance P2P download daemon",
long_about = "A high performance P2P download daemon in Dragonfly that can download resources of different protocols. \
When user triggers a file downloading task, dfdaemon will download the pieces of file from other peers. \
Meanwhile, it will act as an uploader to support other peers to download pieces from it if it owns them."
Meanwhile, it will act as an uploader to support other peers to download pieces from it if it owns them.",
disable_version_flag = true
)]
struct Args {
#[arg(
Expand Down Expand Up @@ -94,6 +96,16 @@ struct Args {
help = "Specify whether to print log"
)]
verbose: bool,

#[arg(
short = 'V',
long = "version",
help = "Print version",
default_value_t = false,
action = clap::ArgAction::SetTrue,
value_parser = DetailedVersionParser
)]
version: bool,
}

#[tokio::main]
Expand Down
Loading

0 comments on commit a9ea307

Please sign in to comment.