Skip to content

Commit 2d4c54d

Browse files
authored
feat: add args for dfcache (#947)
Signed-off-by: Gaius <[email protected]>
1 parent bb86c3b commit 2d4c54d

File tree

5 files changed

+224
-70
lines changed

5 files changed

+224
-70
lines changed

dragonfly-client/src/bin/dfcache/export.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,63 @@ pub struct ExportCommand {
7676
help = "Specify the timeout for exporting a file"
7777
)]
7878
timeout: Duration,
79+
80+
#[arg(
81+
short = 'e',
82+
long = "endpoint",
83+
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
84+
help = "Endpoint of dfdaemon's GRPC server"
85+
)]
86+
endpoint: PathBuf,
87+
88+
#[arg(
89+
short = 'l',
90+
long,
91+
default_value = "info",
92+
help = "Specify the logging level [trace, debug, info, warn, error]"
93+
)]
94+
log_level: Level,
95+
96+
#[arg(
97+
long,
98+
default_value_os_t = dfcache::default_dfcache_log_dir(),
99+
help = "Specify the log directory"
100+
)]
101+
log_dir: PathBuf,
102+
103+
#[arg(
104+
long,
105+
default_value_t = 6,
106+
help = "Specify the max number of log files"
107+
)]
108+
log_max_files: usize,
109+
110+
#[arg(
111+
long = "verbose",
112+
default_value_t = false,
113+
help = "Specify whether to print log"
114+
)]
115+
verbose: bool,
79116
}
80117

81118
/// Implement the execute for ExportCommand.
82119
impl ExportCommand {
83120
/// execute executes the export command.
84-
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
121+
pub async fn execute(&self) -> Result<()> {
122+
// Parse command line arguments.
123+
Args::parse();
124+
125+
// Initialize tracing.
126+
let _guards = init_tracing(
127+
dfcache::NAME,
128+
self.log_dir.clone(),
129+
self.log_level,
130+
self.log_max_files,
131+
None,
132+
false,
133+
self.verbose,
134+
);
135+
85136
// Validate the command line arguments.
86137
if let Err(err) = self.validate_args() {
87138
println!(
@@ -122,7 +173,7 @@ impl ExportCommand {
122173

123174
// Get dfdaemon download client.
124175
let dfdaemon_download_client =
125-
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
176+
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
126177
Ok(client) => client,
127178
Err(err) => {
128179
println!(
@@ -148,7 +199,7 @@ impl ExportCommand {
148199
style::Bold,
149200
style::Reset,
150201
err,
151-
endpoint.to_string_lossy(),
202+
self.endpoint.to_string_lossy(),
152203
);
153204

154205
println!(

dragonfly-client/src/bin/dfcache/import.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use dragonfly_client_core::{
2222
Error, Result,
2323
};
2424
use indicatif::{ProgressBar, ProgressStyle};
25-
use std::path::{Path, PathBuf};
25+
use std::path::PathBuf;
2626
use std::time::Duration;
2727
use termion::{color, style};
2828

@@ -73,12 +73,63 @@ pub struct ImportCommand {
7373
help = "Specify the timeout for importing a file"
7474
)]
7575
timeout: Duration,
76+
77+
#[arg(
78+
short = 'e',
79+
long = "endpoint",
80+
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
81+
help = "Endpoint of dfdaemon's GRPC server"
82+
)]
83+
endpoint: PathBuf,
84+
85+
#[arg(
86+
short = 'l',
87+
long,
88+
default_value = "info",
89+
help = "Specify the logging level [trace, debug, info, warn, error]"
90+
)]
91+
log_level: Level,
92+
93+
#[arg(
94+
long,
95+
default_value_os_t = dfcache::default_dfcache_log_dir(),
96+
help = "Specify the log directory"
97+
)]
98+
log_dir: PathBuf,
99+
100+
#[arg(
101+
long,
102+
default_value_t = 6,
103+
help = "Specify the max number of log files"
104+
)]
105+
log_max_files: usize,
106+
107+
#[arg(
108+
long = "verbose",
109+
default_value_t = false,
110+
help = "Specify whether to print log"
111+
)]
112+
verbose: bool,
76113
}
77114

78115
/// Implement the execute for ImportCommand.
79116
impl ImportCommand {
80117
/// execute executes the import sub command.
81-
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
118+
pub async fn execute(&self) -> Result<()> {
119+
// Parse command line arguments.
120+
Args::parse();
121+
122+
// Initialize tracing.
123+
let _guards = init_tracing(
124+
dfcache::NAME,
125+
self.log_dir.clone(),
126+
self.log_level,
127+
self.log_max_files,
128+
None,
129+
false,
130+
self.verbose,
131+
);
132+
82133
// Validate the command line arguments.
83134
if let Err(err) = self.validate_args() {
84135
println!(
@@ -119,7 +170,7 @@ impl ImportCommand {
119170

120171
// Get dfdaemon download client.
121172
let dfdaemon_download_client =
122-
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
173+
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
123174
Ok(client) => client,
124175
Err(err) => {
125176
println!(
@@ -145,7 +196,7 @@ impl ImportCommand {
145196
style::Bold,
146197
style::Reset,
147198
err,
148-
endpoint.to_string_lossy(),
199+
self.endpoint.to_string_lossy(),
149200
);
150201

151202
println!(

dragonfly-client/src/bin/dfcache/main.rs

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use dragonfly_client::tracing::init_tracing;
2121
use dragonfly_client_config::VersionValueParser;
2222
use dragonfly_client_config::{dfcache, dfdaemon};
2323
use dragonfly_client_core::Result;
24-
use std::path::{Path, PathBuf};
24+
use std::path::PathBuf;
2525
use tracing::Level;
2626

2727
pub mod export;
@@ -40,43 +40,6 @@ pub mod stat;
4040
disable_version_flag = true
4141
)]
4242
struct Args {
43-
#[arg(
44-
short = 'e',
45-
long = "endpoint",
46-
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
47-
help = "Endpoint of dfdaemon's GRPC server"
48-
)]
49-
endpoint: PathBuf,
50-
51-
#[arg(
52-
short = 'l',
53-
long,
54-
default_value = "info",
55-
help = "Specify the logging level [trace, debug, info, warn, error]"
56-
)]
57-
log_level: Level,
58-
59-
#[arg(
60-
long,
61-
default_value_os_t = dfcache::default_dfcache_log_dir(),
62-
help = "Specify the log directory"
63-
)]
64-
log_dir: PathBuf,
65-
66-
#[arg(
67-
long,
68-
default_value_t = 6,
69-
help = "Specify the max number of log files"
70-
)]
71-
log_max_files: usize,
72-
73-
#[arg(
74-
long = "verbose",
75-
default_value_t = false,
76-
help = "Specify whether to print log"
77-
)]
78-
verbose: bool,
79-
8043
#[arg(
8144
short = 'V',
8245
long = "version",
@@ -134,12 +97,12 @@ pub enum Command {
13497
/// Implement the execute for Command.
13598
impl Command {
13699
#[allow(unused)]
137-
pub async fn execute(self, endpoint: &Path) -> Result<()> {
100+
pub async fn execute(self) -> Result<()> {
138101
match self {
139-
Self::Import(cmd) => cmd.execute(endpoint).await,
140-
Self::Export(cmd) => cmd.execute(endpoint).await,
141-
Self::Stat(cmd) => cmd.execute(endpoint).await,
142-
Self::Remove(cmd) => cmd.execute(endpoint).await,
102+
Self::Import(cmd) => cmd.execute().await,
103+
Self::Export(cmd) => cmd.execute().await,
104+
Self::Stat(cmd) => cmd.execute().await,
105+
Self::Remove(cmd) => cmd.execute().await,
143106
}
144107
}
145108
}
@@ -149,19 +112,8 @@ async fn main() -> anyhow::Result<()> {
149112
// Parse command line arguments.
150113
let args = Args::parse();
151114

152-
// Initialize tracing.
153-
let _guards = init_tracing(
154-
dfcache::NAME,
155-
args.log_dir,
156-
args.log_level,
157-
args.log_max_files,
158-
None,
159-
false,
160-
args.verbose,
161-
);
162-
163115
// Execute the command.
164-
args.command.execute(&args.endpoint).await?;
116+
args.command.execute().await?;
165117
Ok(())
166118
}
167119

dragonfly-client/src/bin/dfcache/remove.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use clap::Parser;
1818
use dragonfly_api::dfdaemon::v2::DeletePersistentCacheTaskRequest;
1919
use dragonfly_client_core::{Error, Result};
2020
use indicatif::{ProgressBar, ProgressStyle};
21-
use std::path::Path;
2221
use std::time::Duration;
2322
use termion::{color, style};
2423

@@ -32,15 +31,66 @@ const DEFAULT_PROGRESS_BAR_STEADY_TICK_INTERVAL: Duration = Duration::from_milli
3231
pub struct RemoveCommand {
3332
#[arg(help = "Specify the persistent cache task ID to remove")]
3433
id: String,
34+
35+
#[arg(
36+
short = 'e',
37+
long = "endpoint",
38+
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
39+
help = "Endpoint of dfdaemon's GRPC server"
40+
)]
41+
endpoint: PathBuf,
42+
43+
#[arg(
44+
short = 'l',
45+
long,
46+
default_value = "info",
47+
help = "Specify the logging level [trace, debug, info, warn, error]"
48+
)]
49+
log_level: Level,
50+
51+
#[arg(
52+
long,
53+
default_value_os_t = dfcache::default_dfcache_log_dir(),
54+
help = "Specify the log directory"
55+
)]
56+
log_dir: PathBuf,
57+
58+
#[arg(
59+
long,
60+
default_value_t = 6,
61+
help = "Specify the max number of log files"
62+
)]
63+
log_max_files: usize,
64+
65+
#[arg(
66+
long = "verbose",
67+
default_value_t = false,
68+
help = "Specify whether to print log"
69+
)]
70+
verbose: bool,
3571
}
3672

3773
/// Implement the execute for RemoveCommand.
3874
impl RemoveCommand {
3975
/// execute executes the delete command.
40-
pub async fn execute(&self, endpoint: &Path) -> Result<()> {
76+
pub async fn execute(&self) -> Result<()> {
77+
// Parse command line arguments.
78+
Args::parse();
79+
80+
// Initialize tracing.
81+
let _guards = init_tracing(
82+
dfcache::NAME,
83+
self.log_dir.clone(),
84+
self.log_level,
85+
self.log_max_files,
86+
None,
87+
false,
88+
self.verbose,
89+
);
90+
4191
// Get dfdaemon download client.
4292
let dfdaemon_download_client =
43-
match get_dfdaemon_download_client(endpoint.to_path_buf()).await {
93+
match get_dfdaemon_download_client(self.endpoint.to_path_buf()).await {
4494
Ok(client) => client,
4595
Err(err) => {
4696
println!(
@@ -66,7 +116,7 @@ impl RemoveCommand {
66116
style::Bold,
67117
style::Reset,
68118
err,
69-
endpoint.to_string_lossy(),
119+
self.endpoint.to_string_lossy(),
70120
);
71121

72122
println!(

0 commit comments

Comments
 (0)