Skip to content

Commit 8fa02ff

Browse files
committed
refactor(client-cli): use mutable ConfigParameter from CommandContext
1 parent 1ccc817 commit 8fa02ff

File tree

17 files changed

+150
-140
lines changed

17 files changed

+150
-140
lines changed

mithril-client-cli/src/command_context.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use anyhow::anyhow;
2-
use config::ConfigBuilder;
3-
use config::builder::DefaultState;
42
use slog::Logger;
5-
use std::collections::HashMap;
63

74
use mithril_client::MithrilResult;
85

96
use crate::configuration::ConfigParameters;
107

118
/// Context for the command execution
129
pub struct CommandContext {
13-
config_builder: ConfigBuilder<DefaultState>,
10+
config_parameters: ConfigParameters,
1411
unstable_enabled: bool,
1512
json: bool,
1613
logger: Logger,
@@ -19,13 +16,13 @@ pub struct CommandContext {
1916
impl CommandContext {
2017
/// Create a new command context
2118
pub fn new(
22-
config_builder: ConfigBuilder<DefaultState>,
19+
config_parameters: ConfigParameters,
2320
unstable_enabled: bool,
2421
json: bool,
2522
logger: Logger,
2623
) -> Self {
2724
Self {
28-
config_builder,
25+
config_parameters,
2926
unstable_enabled,
3027
json,
3128
logger,
@@ -59,11 +56,14 @@ impl CommandContext {
5956
}
6057
}
6158

62-
/// Get the configured parameters
63-
pub fn config_parameters(&self) -> MithrilResult<ConfigParameters> {
64-
let config = self.config_builder.clone().build()?;
65-
let config_hash_map = config.try_deserialize::<HashMap<String, String>>()?;
66-
Ok(ConfigParameters::new(config_hash_map))
59+
/// Get a reference to the configured parameters
60+
pub fn config_parameters(&self) -> &ConfigParameters {
61+
&self.config_parameters
62+
}
63+
64+
/// Get a mutable reference to the configured parameters
65+
pub fn config_parameters_mut(&mut self) -> &mut ConfigParameters {
66+
&mut self.config_parameters
6767
}
6868

6969
/// Get the shared logger
@@ -75,14 +75,17 @@ impl CommandContext {
7575
#[cfg(test)]
7676
mod tests {
7777
use slog::o;
78+
use std::collections::HashMap;
79+
80+
use crate::configuration::{ConfigError, ConfigSource};
7881

7982
use super::*;
8083

8184
#[test]
8285
fn require_unstable_return_ok_if_unstable_enabled() {
8386
let unstable_enabled = true;
8487
let context = CommandContext::new(
85-
ConfigBuilder::default(),
88+
ConfigParameters::default(),
8689
unstable_enabled,
8790
true,
8891
Logger::root(slog::Discard, o!()),
@@ -96,7 +99,7 @@ mod tests {
9699
fn require_unstable_return_err_if_unstable_disabled() {
97100
let unstable_enabled = false;
98101
let context = CommandContext::new(
99-
ConfigBuilder::default(),
102+
ConfigParameters::default(),
100103
unstable_enabled,
101104
true,
102105
Logger::root(slog::Discard, o!()),
@@ -105,4 +108,39 @@ mod tests {
105108
let result = context.require_unstable("test", None);
106109
assert!(result.is_err(), "Expected Err, got {result:?}");
107110
}
111+
112+
#[test]
113+
fn can_edit_config_parameters() {
114+
struct ParamSource {
115+
key: String,
116+
value: String,
117+
}
118+
impl ConfigSource for ParamSource {
119+
fn collect(&self) -> Result<HashMap<String, String>, ConfigError> {
120+
Ok(HashMap::from([(self.key.clone(), self.value.clone())]))
121+
}
122+
}
123+
124+
let mut context = CommandContext::new(
125+
ConfigParameters::default(),
126+
false,
127+
true,
128+
Logger::root(slog::Discard, o!()),
129+
);
130+
131+
assert_eq!(context.config_parameters_mut().get("key"), None,);
132+
133+
context
134+
.config_parameters_mut()
135+
.add_source(&ParamSource {
136+
key: "key".to_string(),
137+
value: "value".to_string(),
138+
})
139+
.unwrap();
140+
141+
assert_eq!(
142+
context.config_parameters_mut().get("key"),
143+
Some("value".to_string())
144+
);
145+
}
108146
}

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

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{collections::HashMap, path::PathBuf};
1010
use crate::{
1111
CommandContext,
1212
commands::cardano_db::CardanoDbCommandsBackend,
13-
configuration::{ConfigError, ConfigParameters, ConfigSource},
13+
configuration::{ConfigError, ConfigSource},
1414
utils::{self, JSON_CAUTION_KEY},
1515
};
1616
use mithril_client::{MithrilResult, common::ImmutableFileNumber};
@@ -71,62 +71,54 @@ pub struct CardanoDbDownloadCommand {
7171

7272
impl CardanoDbDownloadCommand {
7373
/// Command execution
74-
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
75-
let params = context.config_parameters()?.add_source(self)?;
74+
pub async fn execute(&self, mut context: CommandContext) -> MithrilResult<()> {
75+
context.config_parameters_mut().add_source(self)?;
7676

7777
match self.backend {
7878
CardanoDbCommandsBackend::V1 => {
79-
let prepared_command = self.prepare_v1(&params, &context)?;
80-
prepared_command.execute(&context, params).await
79+
let prepared_command = self.prepare_v1(&context)?;
80+
prepared_command.execute(&context).await
8181
}
8282
CardanoDbCommandsBackend::V2 => {
83-
let prepared_command = self.prepare_v2(&params, &context)?;
84-
prepared_command.execute(&context, params).await
83+
let prepared_command = self.prepare_v2(&context)?;
84+
prepared_command.execute(&context).await
8585
}
8686
}
8787
}
8888

89-
fn prepare_v1(
90-
&self,
91-
params: &ConfigParameters,
92-
context: &CommandContext,
93-
) -> MithrilResult<PreparedCardanoDbV1Download> {
89+
fn prepare_v1(&self, context: &CommandContext) -> MithrilResult<PreparedCardanoDbV1Download> {
9490
if self.allow_override || self.start.is_some() || self.end.is_some() {
9591
self.warn_unused_parameter_with_v1_backend(context);
9692
}
9793

9894
let ancillary_verification_key = if self.include_ancillary {
9995
self.warn_ancillary_not_signed_by_mithril(context);
100-
Some(params.require("ancillary_verification_key")?)
96+
Some(context.config_parameters().require("ancillary_verification_key")?)
10197
} else {
10298
self.warn_fast_bootstrap_not_available(context);
10399
None
104100
};
105101

106102
Ok(PreparedCardanoDbV1Download {
107103
digest: self.digest.clone(),
108-
download_dir: params.require("download_dir")?,
104+
download_dir: context.config_parameters().require("download_dir")?,
109105
include_ancillary: self.include_ancillary,
110106
ancillary_verification_key,
111107
})
112108
}
113109

114-
fn prepare_v2(
115-
&self,
116-
params: &ConfigParameters,
117-
context: &CommandContext,
118-
) -> MithrilResult<PreparedCardanoDbV2Download> {
110+
fn prepare_v2(&self, context: &CommandContext) -> MithrilResult<PreparedCardanoDbV2Download> {
119111
let ancillary_verification_key = if self.include_ancillary {
120112
self.warn_ancillary_not_signed_by_mithril(context);
121-
Some(params.require("ancillary_verification_key")?)
113+
Some(context.config_parameters().require("ancillary_verification_key")?)
122114
} else {
123115
self.warn_fast_bootstrap_not_available(context);
124116
None
125117
};
126118

127119
Ok(PreparedCardanoDbV2Download {
128120
hash: self.digest.clone(),
129-
download_dir: params.require("download_dir")?,
121+
download_dir: context.config_parameters().require("download_dir")?,
130122
start: self.start,
131123
end: self.end,
132124
include_ancillary: self.include_ancillary,
@@ -217,9 +209,10 @@ impl ConfigSource for CardanoDbDownloadCommand {
217209

218210
#[cfg(test)]
219211
mod tests {
220-
use config::ConfigBuilder;
221212
use slog::Logger;
222213

214+
use crate::ConfigParameters;
215+
223216
use super::*;
224217

225218
fn dummy_command() -> CardanoDbDownloadCommand {
@@ -244,7 +237,7 @@ mod tests {
244237
..dummy_command()
245238
};
246239
let command_context = CommandContext::new(
247-
ConfigBuilder::default(),
240+
ConfigParameters::default(),
248241
false,
249242
true,
250243
Logger::root(slog::Discard, slog::o!()),
@@ -268,18 +261,15 @@ mod tests {
268261
ancillary_verification_key: None,
269262
..dummy_command()
270263
};
271-
let config = config::Config::builder()
272-
.set_default("ancillary_verification_key", "value from config")
273-
.expect("Failed to build config builder");
274-
let command_context =
264+
let config = ConfigParameters::new(HashMap::from([(
265+
"ancillary_verification_key".to_string(),
266+
"value from config".to_string(),
267+
)]));
268+
let mut command_context =
275269
CommandContext::new(config, false, true, Logger::root(slog::Discard, slog::o!()));
276-
let config_parameters = command_context
277-
.config_parameters()
278-
.unwrap()
279-
.add_source(&command)
280-
.unwrap();
270+
command_context.config_parameters_mut().add_source(&command).unwrap();
281271

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

284274
assert!(result.is_ok());
285275
}
@@ -290,19 +280,16 @@ mod tests {
290280
download_dir: None,
291281
..dummy_command()
292282
};
293-
let command_context = &CommandContext::new(
294-
ConfigBuilder::default(),
283+
let mut command_context = CommandContext::new(
284+
ConfigParameters::default(),
295285
false,
296286
true,
297287
Logger::root(slog::Discard, slog::o!()),
298288
);
299-
let config_parameters = command_context
300-
.config_parameters()
301-
.unwrap()
302-
.add_source(&command)
303-
.unwrap();
304289

305-
let result = command.prepare_v1(&config_parameters, command_context);
290+
command_context.config_parameters_mut().add_source(&command).unwrap();
291+
292+
let result = command.prepare_v1(&command_context);
306293

307294
assert!(result.is_err());
308295
assert_eq!(
@@ -321,18 +308,16 @@ mod tests {
321308
ancillary_verification_key: None,
322309
..dummy_command()
323310
};
324-
let config = config::Config::builder()
325-
.set_default("ancillary_verification_key", "value from config")
326-
.expect("Failed to build config builder");
327-
let command_context =
311+
let config = ConfigParameters::new(HashMap::from([(
312+
"ancillary_verification_key".to_string(),
313+
"value from config".to_string(),
314+
)]));
315+
let mut command_context =
328316
CommandContext::new(config, false, true, Logger::root(slog::Discard, slog::o!()));
329-
let config_parameters = command_context
330-
.config_parameters()
331-
.unwrap()
332-
.add_source(&command)
333-
.unwrap();
334317

335-
let result = command.prepare_v2(&config_parameters, &command_context);
318+
command_context.config_parameters_mut().add_source(&command).unwrap();
319+
320+
let result = command.prepare_v2(&command_context);
336321

337322
assert!(result.is_ok());
338323
}
@@ -343,19 +328,16 @@ mod tests {
343328
download_dir: None,
344329
..dummy_command()
345330
};
346-
let command_context = CommandContext::new(
347-
ConfigBuilder::default(),
331+
let mut command_context = CommandContext::new(
332+
ConfigParameters::default(),
348333
false,
349334
true,
350335
Logger::root(slog::Discard, slog::o!()),
351336
);
352-
let config_parameters = command_context
353-
.config_parameters()
354-
.unwrap()
355-
.add_source(&command)
356-
.unwrap();
357337

358-
let result = command.prepare_v2(&config_parameters, &command_context);
338+
command_context.config_parameters_mut().add_source(&command).unwrap();
339+
340+
let result = command.prepare_v2(&command_context);
359341

360342
assert!(result.is_err());
361343
assert_eq!(

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
cardano_db::{download::DB_DIRECTORY_NAME, shared_steps},
1414
client_builder,
1515
},
16-
configuration::ConfigParameters,
1716
utils::{
1817
CardanoDbDownloadChecker, CardanoDbUtils, ExpanderUtils, IndicatifFeedbackReceiver,
1918
ProgressOutputType, ProgressPrinter,
@@ -30,11 +29,7 @@ pub(super) struct PreparedCardanoDbV1Download {
3029

3130
impl PreparedCardanoDbV1Download {
3231
/// Command execution
33-
pub async fn execute(
34-
&self,
35-
context: &CommandContext,
36-
params: ConfigParameters,
37-
) -> MithrilResult<()> {
32+
pub async fn execute(&self, context: &CommandContext) -> MithrilResult<()> {
3833
let db_dir = Path::new(&self.download_dir).join(DB_DIRECTORY_NAME);
3934

4035
let progress_output_type = if context.is_json_output_enabled() {
@@ -43,7 +38,7 @@ impl PreparedCardanoDbV1Download {
4338
ProgressOutputType::Tty
4439
};
4540
let progress_printer = ProgressPrinter::new(progress_output_type, 5);
46-
let client = client_builder(&params)?
41+
let client = client_builder(context.config_parameters())?
4742
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
4843
progress_output_type,
4944
context.logger().clone(),

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::{
1818
cardano_db::{download::DB_DIRECTORY_NAME, shared_steps},
1919
client_builder,
2020
},
21-
configuration::ConfigParameters,
2221
utils::{
2322
CardanoDbDownloadChecker, CardanoDbUtils, ExpanderUtils, IndicatifFeedbackReceiver,
2423
ProgressOutputType, ProgressPrinter,
@@ -46,11 +45,7 @@ pub(super) struct PreparedCardanoDbV2Download {
4645
}
4746

4847
impl PreparedCardanoDbV2Download {
49-
pub async fn execute(
50-
&self,
51-
context: &CommandContext,
52-
params: ConfigParameters,
53-
) -> MithrilResult<()> {
48+
pub async fn execute(&self, context: &CommandContext) -> MithrilResult<()> {
5449
let restoration_options = RestorationOptions {
5550
db_dir: Path::new(&self.download_dir).join(DB_DIRECTORY_NAME),
5651
immutable_file_range: shared_steps::immutable_file_range(self.start, self.end),
@@ -68,7 +63,7 @@ impl PreparedCardanoDbV2Download {
6863
ProgressOutputType::Tty
6964
};
7065
let progress_printer = ProgressPrinter::new(progress_output_type, 6);
71-
let client = client_builder(&params)?
66+
let client = client_builder(context.config_parameters())?
7267
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
7368
progress_output_type,
7469
context.logger().clone(),

mithril-client-cli/src/commands/cardano_db/list.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ pub struct CardanoDbListCommand {
1818
impl CardanoDbListCommand {
1919
/// Main command execution
2020
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
21-
let params = context.config_parameters()?;
22-
let client = client_builder_with_fallback_genesis_key(&params)?
21+
let client = client_builder_with_fallback_genesis_key(context.config_parameters())?
2322
.with_logger(context.logger().clone())
2423
.build()?;
2524

0 commit comments

Comments
 (0)