Skip to content

Commit 1ccc817

Browse files
committed
refactor(client-cli): move json boolean to commandContext and remove
sharedArgs
1 parent 6833d2f commit 1ccc817

File tree

17 files changed

+122
-211
lines changed

17 files changed

+122
-211
lines changed

mithril-client-cli/src/command_context.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::configuration::ConfigParameters;
1212
pub struct CommandContext {
1313
config_builder: ConfigBuilder<DefaultState>,
1414
unstable_enabled: bool,
15+
json: bool,
1516
logger: Logger,
1617
}
1718

@@ -20,11 +21,13 @@ impl CommandContext {
2021
pub fn new(
2122
config_builder: ConfigBuilder<DefaultState>,
2223
unstable_enabled: bool,
24+
json: bool,
2325
logger: Logger,
2426
) -> Self {
2527
Self {
2628
config_builder,
2729
unstable_enabled,
30+
json,
2831
logger,
2932
}
3033
}
@@ -34,6 +37,11 @@ impl CommandContext {
3437
self.unstable_enabled
3538
}
3639

40+
/// Check if JSON output is enabled
41+
pub fn is_json_output_enabled(&self) -> bool {
42+
self.json
43+
}
44+
3745
/// Ensure that unstable commands are enabled
3846
pub fn require_unstable(
3947
&self,
@@ -76,6 +84,7 @@ mod tests {
7684
let context = CommandContext::new(
7785
ConfigBuilder::default(),
7886
unstable_enabled,
87+
true,
7988
Logger::root(slog::Discard, o!()),
8089
);
8190

@@ -89,6 +98,7 @@ mod tests {
8998
let context = CommandContext::new(
9099
ConfigBuilder::default(),
91100
unstable_enabled,
101+
true,
92102
Logger::root(slog::Discard, o!()),
93103
);
94104

mithril-client-cli/src/commands/cardano_db/download/mod.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{collections::HashMap, path::PathBuf};
99

1010
use crate::{
1111
CommandContext,
12-
commands::SharedArgs,
1312
commands::cardano_db::CardanoDbCommandsBackend,
1413
configuration::{ConfigError, ConfigParameters, ConfigSource},
1514
utils::{self, JSON_CAUTION_KEY},
@@ -24,9 +23,6 @@ pub struct CardanoDbDownloadCommand {
2423
#[arg(short, long, value_enum, default_value_t)]
2524
backend: CardanoDbCommandsBackend,
2625

27-
#[clap(flatten)]
28-
shared_args: SharedArgs,
29-
3026
/// Digest of the Cardano db snapshot to download or `latest` for the latest artifact
3127
///
3228
/// Use the `list` command to get that information.
@@ -74,59 +70,61 @@ pub struct CardanoDbDownloadCommand {
7470
}
7571

7672
impl CardanoDbDownloadCommand {
77-
fn is_json_output_enabled(&self) -> bool {
78-
self.shared_args.json
79-
}
80-
8173
/// Command execution
8274
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
8375
let params = context.config_parameters()?.add_source(self)?;
8476

8577
match self.backend {
8678
CardanoDbCommandsBackend::V1 => {
87-
let prepared_command = self.prepare_v1(&params)?;
88-
prepared_command.execute(context.logger(), params).await
79+
let prepared_command = self.prepare_v1(&params, &context)?;
80+
prepared_command.execute(&context, params).await
8981
}
9082
CardanoDbCommandsBackend::V2 => {
91-
let prepared_command = self.prepare_v2(&params)?;
92-
prepared_command.execute(context.logger(), params).await
83+
let prepared_command = self.prepare_v2(&params, &context)?;
84+
prepared_command.execute(&context, params).await
9385
}
9486
}
9587
}
9688

97-
fn prepare_v1(&self, params: &ConfigParameters) -> MithrilResult<PreparedCardanoDbV1Download> {
89+
fn prepare_v1(
90+
&self,
91+
params: &ConfigParameters,
92+
context: &CommandContext,
93+
) -> MithrilResult<PreparedCardanoDbV1Download> {
9894
if self.allow_override || self.start.is_some() || self.end.is_some() {
99-
self.warn_unused_parameter_with_v1_backend();
95+
self.warn_unused_parameter_with_v1_backend(context);
10096
}
10197

10298
let ancillary_verification_key = if self.include_ancillary {
103-
self.warn_ancillary_not_signed_by_mithril();
99+
self.warn_ancillary_not_signed_by_mithril(context);
104100
Some(params.require("ancillary_verification_key")?)
105101
} else {
106-
self.warn_fast_bootstrap_not_available();
102+
self.warn_fast_bootstrap_not_available(context);
107103
None
108104
};
109105

110106
Ok(PreparedCardanoDbV1Download {
111-
shared_args: self.shared_args.clone(),
112107
digest: self.digest.clone(),
113108
download_dir: params.require("download_dir")?,
114109
include_ancillary: self.include_ancillary,
115110
ancillary_verification_key,
116111
})
117112
}
118113

119-
fn prepare_v2(&self, params: &ConfigParameters) -> MithrilResult<PreparedCardanoDbV2Download> {
114+
fn prepare_v2(
115+
&self,
116+
params: &ConfigParameters,
117+
context: &CommandContext,
118+
) -> MithrilResult<PreparedCardanoDbV2Download> {
120119
let ancillary_verification_key = if self.include_ancillary {
121-
self.warn_ancillary_not_signed_by_mithril();
120+
self.warn_ancillary_not_signed_by_mithril(context);
122121
Some(params.require("ancillary_verification_key")?)
123122
} else {
124-
self.warn_fast_bootstrap_not_available();
123+
self.warn_fast_bootstrap_not_available(context);
125124
None
126125
};
127126

128127
Ok(PreparedCardanoDbV2Download {
129-
shared_args: self.shared_args.clone(),
130128
hash: self.digest.clone(),
131129
download_dir: params.require("download_dir")?,
132130
start: self.start,
@@ -138,8 +136,8 @@ impl CardanoDbDownloadCommand {
138136
}
139137

140138
/// Provides guidance on how to enable fast bootstrap by including ancillary files
141-
fn warn_fast_bootstrap_not_available(&self) {
142-
if self.is_json_output_enabled() {
139+
fn warn_fast_bootstrap_not_available(&self, context: &CommandContext) {
140+
if context.is_json_output_enabled() {
143141
let json = serde_json::json!({
144142
JSON_CAUTION_KEY: "The fast bootstrap of the Cardano node is not available with the current parameters used in this command",
145143
"impact": "The ledger state will be recomputed from genesis at startup of the Cardano node",
@@ -165,18 +163,18 @@ For more information, please refer to the network configuration page of the docu
165163
}
166164
}
167165

168-
fn warn_ancillary_not_signed_by_mithril(&self) {
166+
fn warn_ancillary_not_signed_by_mithril(&self, context: &CommandContext) {
169167
let message = "Ancillary verification does not use the Mithril certification: as a mitigation, IOG owned keys are used to sign these files.";
170-
if self.is_json_output_enabled() {
168+
if context.is_json_output_enabled() {
171169
eprintln!(r#"{{"{JSON_CAUTION_KEY}":"{message}"}}"#);
172170
} else {
173171
eprintln!("{message}");
174172
}
175173
}
176174

177-
fn warn_unused_parameter_with_v1_backend(&self) {
175+
fn warn_unused_parameter_with_v1_backend(&self, context: &CommandContext) {
178176
let message = "`--start`, `--end`, and `--allow-override` are only available with the `v2` backend. They will be ignored.";
179-
if self.is_json_output_enabled() {
177+
if context.is_json_output_enabled() {
180178
eprintln!(r#"{{"{JSON_CAUTION_KEY}":"{message}"}}"#);
181179
} else {
182180
eprintln!("{message}");
@@ -227,7 +225,6 @@ mod tests {
227225
fn dummy_command() -> CardanoDbDownloadCommand {
228226
CardanoDbDownloadCommand {
229227
backend: Default::default(),
230-
shared_args: SharedArgs { json: false },
231228
digest: "whatever_digest".to_string(),
232229
download_dir: Some(std::path::PathBuf::from("whatever_dir")),
233230
genesis_verification_key: "whatever".to_string().into(),
@@ -249,6 +246,7 @@ mod tests {
249246
let command_context = CommandContext::new(
250247
ConfigBuilder::default(),
251248
false,
249+
true,
252250
Logger::root(slog::Discard, slog::o!()),
253251
);
254252

@@ -274,14 +272,14 @@ mod tests {
274272
.set_default("ancillary_verification_key", "value from config")
275273
.expect("Failed to build config builder");
276274
let command_context =
277-
CommandContext::new(config, false, Logger::root(slog::Discard, slog::o!()));
275+
CommandContext::new(config, false, true, Logger::root(slog::Discard, slog::o!()));
278276
let config_parameters = command_context
279277
.config_parameters()
280278
.unwrap()
281279
.add_source(&command)
282280
.unwrap();
283281

284-
let result = command.prepare_v1(&config_parameters);
282+
let result = command.prepare_v1(&config_parameters, &command_context);
285283

286284
assert!(result.is_ok());
287285
}
@@ -292,9 +290,10 @@ mod tests {
292290
download_dir: None,
293291
..dummy_command()
294292
};
295-
let command_context = CommandContext::new(
293+
let command_context = &CommandContext::new(
296294
ConfigBuilder::default(),
297295
false,
296+
true,
298297
Logger::root(slog::Discard, slog::o!()),
299298
);
300299
let config_parameters = command_context
@@ -303,7 +302,7 @@ mod tests {
303302
.add_source(&command)
304303
.unwrap();
305304

306-
let result = command.prepare_v1(&config_parameters);
305+
let result = command.prepare_v1(&config_parameters, command_context);
307306

308307
assert!(result.is_err());
309308
assert_eq!(
@@ -326,14 +325,14 @@ mod tests {
326325
.set_default("ancillary_verification_key", "value from config")
327326
.expect("Failed to build config builder");
328327
let command_context =
329-
CommandContext::new(config, false, Logger::root(slog::Discard, slog::o!()));
328+
CommandContext::new(config, false, true, Logger::root(slog::Discard, slog::o!()));
330329
let config_parameters = command_context
331330
.config_parameters()
332331
.unwrap()
333332
.add_source(&command)
334333
.unwrap();
335334

336-
let result = command.prepare_v2(&config_parameters);
335+
let result = command.prepare_v2(&config_parameters, &command_context);
337336

338337
assert!(result.is_ok());
339338
}
@@ -347,6 +346,7 @@ mod tests {
347346
let command_context = CommandContext::new(
348347
ConfigBuilder::default(),
349348
false,
349+
true,
350350
Logger::root(slog::Discard, slog::o!()),
351351
);
352352
let config_parameters = command_context
@@ -355,7 +355,7 @@ mod tests {
355355
.add_source(&command)
356356
.unwrap();
357357

358-
let result = command.prepare_v2(&config_parameters);
358+
let result = command.prepare_v2(&config_parameters, &command_context);
359359

360360
assert!(result.is_err());
361361
assert_eq!(

mithril-client-cli/src/commands/cardano_db/download/v1.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use mithril_client::{
88
};
99

1010
use crate::{
11+
CommandContext,
1112
commands::{
12-
SharedArgs,
1313
cardano_db::{download::DB_DIRECTORY_NAME, shared_steps},
1414
client_builder,
1515
},
@@ -22,23 +22,22 @@ use crate::{
2222

2323
#[derive(Debug, Clone)]
2424
pub(super) struct PreparedCardanoDbV1Download {
25-
pub(super) shared_args: SharedArgs,
2625
pub(super) digest: String,
2726
pub(super) download_dir: String,
2827
pub(super) include_ancillary: bool,
2928
pub(super) ancillary_verification_key: Option<String>,
3029
}
3130

3231
impl PreparedCardanoDbV1Download {
33-
fn is_json_output_enabled(&self) -> bool {
34-
self.shared_args.json
35-
}
36-
3732
/// Command execution
38-
pub async fn execute(&self, logger: &Logger, params: ConfigParameters) -> MithrilResult<()> {
33+
pub async fn execute(
34+
&self,
35+
context: &CommandContext,
36+
params: ConfigParameters,
37+
) -> MithrilResult<()> {
3938
let db_dir = Path::new(&self.download_dir).join(DB_DIRECTORY_NAME);
4039

41-
let progress_output_type = if self.is_json_output_enabled() {
40+
let progress_output_type = if context.is_json_output_enabled() {
4241
ProgressOutputType::JsonReporter
4342
} else {
4443
ProgressOutputType::Tty
@@ -47,10 +46,10 @@ impl PreparedCardanoDbV1Download {
4746
let client = client_builder(&params)?
4847
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
4948
progress_output_type,
50-
logger.clone(),
49+
context.logger().clone(),
5150
)))
5251
.set_ancillary_verification_key(self.ancillary_verification_key.clone())
53-
.with_logger(logger.clone())
52+
.with_logger(context.logger().clone())
5453
.build()?;
5554

5655
let get_list_of_artifact_ids = || async {
@@ -84,7 +83,7 @@ impl PreparedCardanoDbV1Download {
8483
.await?;
8584

8685
Self::download_and_unpack_cardano_db(
87-
logger,
86+
context.logger(),
8887
3,
8988
&progress_printer,
9089
client.cardano_database(),
@@ -104,7 +103,7 @@ impl PreparedCardanoDbV1Download {
104103
Self::compute_cardano_db_message(4, &progress_printer, &certificate, &db_dir).await?;
105104

106105
Self::verify_cardano_db_signature(
107-
logger,
106+
context.logger(),
108107
5,
109108
&progress_printer,
110109
&certificate,
@@ -119,7 +118,7 @@ impl PreparedCardanoDbV1Download {
119118
&cardano_db_message.digest,
120119
&cardano_db_message.network,
121120
&cardano_db_message.cardano_node_version,
122-
self.is_json_output_enabled(),
121+
context.is_json_output_enabled(),
123122
self.include_ancillary,
124123
)?;
125124

0 commit comments

Comments
 (0)