Skip to content

Commit

Permalink
feat: add args for dfcache (#947)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Jan 20, 2025
1 parent bb86c3b commit 2d4c54d
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 70 deletions.
57 changes: 54 additions & 3 deletions dragonfly-client/src/bin/dfcache/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,63 @@ pub struct ExportCommand {
help = "Specify the timeout for exporting a file"
)]
timeout: Duration,

#[arg(
short = 'e',
long = "endpoint",
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
help = "Endpoint of dfdaemon's GRPC server"
)]
endpoint: PathBuf,

#[arg(
short = 'l',
long,
default_value = "info",
help = "Specify the logging level [trace, debug, info, warn, error]"
)]
log_level: Level,

#[arg(
long,
default_value_os_t = dfcache::default_dfcache_log_dir(),
help = "Specify the log directory"
)]
log_dir: PathBuf,

#[arg(
long,
default_value_t = 6,
help = "Specify the max number of log files"
)]
log_max_files: usize,

#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
}

/// Implement the execute for ExportCommand.
impl ExportCommand {
/// execute executes the export command.
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();

// Initialize tracing.
let _guards = init_tracing(
dfcache::NAME,
self.log_dir.clone(),
self.log_level,
self.log_max_files,
None,
false,
self.verbose,
);

// Validate the command line arguments.
if let Err(err) = self.validate_args() {
println!(
Expand Down Expand Up @@ -122,7 +173,7 @@ impl ExportCommand {

// Get dfdaemon download client.
let dfdaemon_download_client =
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
Ok(client) => client,
Err(err) => {
println!(
Expand All @@ -148,7 +199,7 @@ impl ExportCommand {
style::Bold,
style::Reset,
err,
endpoint.to_string_lossy(),
self.endpoint.to_string_lossy(),
);

println!(
Expand Down
59 changes: 55 additions & 4 deletions dragonfly-client/src/bin/dfcache/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use dragonfly_client_core::{
Error, Result,
};
use indicatif::{ProgressBar, ProgressStyle};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::time::Duration;
use termion::{color, style};

Expand Down Expand Up @@ -73,12 +73,63 @@ pub struct ImportCommand {
help = "Specify the timeout for importing a file"
)]
timeout: Duration,

#[arg(
short = 'e',
long = "endpoint",
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
help = "Endpoint of dfdaemon's GRPC server"
)]
endpoint: PathBuf,

#[arg(
short = 'l',
long,
default_value = "info",
help = "Specify the logging level [trace, debug, info, warn, error]"
)]
log_level: Level,

#[arg(
long,
default_value_os_t = dfcache::default_dfcache_log_dir(),
help = "Specify the log directory"
)]
log_dir: PathBuf,

#[arg(
long,
default_value_t = 6,
help = "Specify the max number of log files"
)]
log_max_files: usize,

#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
}

/// Implement the execute for ImportCommand.
impl ImportCommand {
/// execute executes the import sub command.
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();

// Initialize tracing.
let _guards = init_tracing(
dfcache::NAME,
self.log_dir.clone(),
self.log_level,
self.log_max_files,
None,
false,
self.verbose,
);

// Validate the command line arguments.
if let Err(err) = self.validate_args() {
println!(
Expand Down Expand Up @@ -119,7 +170,7 @@ impl ImportCommand {

// Get dfdaemon download client.
let dfdaemon_download_client =
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
Ok(client) => client,
Err(err) => {
println!(
Expand All @@ -145,7 +196,7 @@ impl ImportCommand {
style::Bold,
style::Reset,
err,
endpoint.to_string_lossy(),
self.endpoint.to_string_lossy(),
);

println!(
Expand Down
62 changes: 7 additions & 55 deletions dragonfly-client/src/bin/dfcache/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use dragonfly_client::tracing::init_tracing;
use dragonfly_client_config::VersionValueParser;
use dragonfly_client_config::{dfcache, dfdaemon};
use dragonfly_client_core::Result;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use tracing::Level;

pub mod export;
Expand All @@ -40,43 +40,6 @@ pub mod stat;
disable_version_flag = true
)]
struct Args {
#[arg(
short = 'e',
long = "endpoint",
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
help = "Endpoint of dfdaemon's GRPC server"
)]
endpoint: PathBuf,

#[arg(
short = 'l',
long,
default_value = "info",
help = "Specify the logging level [trace, debug, info, warn, error]"
)]
log_level: Level,

#[arg(
long,
default_value_os_t = dfcache::default_dfcache_log_dir(),
help = "Specify the log directory"
)]
log_dir: PathBuf,

#[arg(
long,
default_value_t = 6,
help = "Specify the max number of log files"
)]
log_max_files: usize,

#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,

#[arg(
short = 'V',
long = "version",
Expand Down Expand Up @@ -134,12 +97,12 @@ pub enum Command {
/// Implement the execute for Command.
impl Command {
#[allow(unused)]
pub async fn execute(self, endpoint: &Path) -> Result<()> {
pub async fn execute(self) -> Result<()> {
match self {
Self::Import(cmd) => cmd.execute(endpoint).await,
Self::Export(cmd) => cmd.execute(endpoint).await,
Self::Stat(cmd) => cmd.execute(endpoint).await,
Self::Remove(cmd) => cmd.execute(endpoint).await,
Self::Import(cmd) => cmd.execute().await,
Self::Export(cmd) => cmd.execute().await,
Self::Stat(cmd) => cmd.execute().await,
Self::Remove(cmd) => cmd.execute().await,
}
}
}
Expand All @@ -149,19 +112,8 @@ async fn main() -> anyhow::Result<()> {
// Parse command line arguments.
let args = Args::parse();

// Initialize tracing.
let _guards = init_tracing(
dfcache::NAME,
args.log_dir,
args.log_level,
args.log_max_files,
None,
false,
args.verbose,
);

// Execute the command.
args.command.execute(&args.endpoint).await?;
args.command.execute().await?;
Ok(())
}

Expand Down
58 changes: 54 additions & 4 deletions dragonfly-client/src/bin/dfcache/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use clap::Parser;
use dragonfly_api::dfdaemon::v2::DeletePersistentCacheTaskRequest;
use dragonfly_client_core::{Error, Result};
use indicatif::{ProgressBar, ProgressStyle};
use std::path::Path;
use std::time::Duration;
use termion::{color, style};

Expand All @@ -32,15 +31,66 @@ const DEFAULT_PROGRESS_BAR_STEADY_TICK_INTERVAL: Duration = Duration::from_milli
pub struct RemoveCommand {
#[arg(help = "Specify the persistent cache task ID to remove")]
id: String,

#[arg(
short = 'e',
long = "endpoint",
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
help = "Endpoint of dfdaemon's GRPC server"
)]
endpoint: PathBuf,

#[arg(
short = 'l',
long,
default_value = "info",
help = "Specify the logging level [trace, debug, info, warn, error]"
)]
log_level: Level,

#[arg(
long,
default_value_os_t = dfcache::default_dfcache_log_dir(),
help = "Specify the log directory"
)]
log_dir: PathBuf,

#[arg(
long,
default_value_t = 6,
help = "Specify the max number of log files"
)]
log_max_files: usize,

#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
}

/// Implement the execute for RemoveCommand.
impl RemoveCommand {
/// execute executes the delete command.
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();

// Initialize tracing.
let _guards = init_tracing(
dfcache::NAME,
self.log_dir.clone(),
self.log_level,
self.log_max_files,
None,
false,
self.verbose,
);

// Get dfdaemon download client.
let dfdaemon_download_client =
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
Ok(client) => client,
Err(err) => {
println!(
Expand All @@ -66,7 +116,7 @@ impl RemoveCommand {
style::Bold,
style::Reset,
err,
endpoint.to_string_lossy(),
self.endpoint.to_string_lossy(),
);

println!(
Expand Down
Loading

0 comments on commit 2d4c54d

Please sign in to comment.