Skip to content

Commit 786c9e7

Browse files
committed
refactor(client-cli): make unused param in v2 warning reusable
- move it from the cardano db download module to its parent module so all cardano db commmands can use it. - make the unused parameters list configurable
1 parent 70e5b8b commit 786c9e7

File tree

2 files changed

+67
-13
lines changed
  • mithril-client-cli/src/commands/cardano_db

2 files changed

+67
-13
lines changed

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

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

1010
use crate::{
1111
CommandContext,
12-
commands::cardano_db::CardanoDbCommandsBackend,
12+
commands::cardano_db::{CardanoDbCommandsBackend, warn_unused_parameter_with_v1_backend},
1313
configuration::{ConfigError, ConfigSource},
1414
utils::{self, JSON_CAUTION_KEY},
1515
};
@@ -89,7 +89,10 @@ impl CardanoDbDownloadCommand {
8989

9090
fn prepare_v1(&self, context: &CommandContext) -> MithrilResult<PreparedCardanoDbV1Download> {
9191
if self.allow_override || self.start.is_some() || self.end.is_some() {
92-
self.warn_unused_parameter_with_v1_backend(context);
92+
warn_unused_parameter_with_v1_backend(
93+
context,
94+
["--start", "--end", "--allow_override"],
95+
);
9396
}
9497

9598
let ancillary_verification_key = if self.include_ancillary {
@@ -164,17 +167,6 @@ For more information, please refer to the network configuration page of the docu
164167
eprintln!("{message}");
165168
}
166169
}
167-
168-
fn warn_unused_parameter_with_v1_backend(&self, context: &CommandContext) {
169-
let message = "`--start`, `--end`, and `--allow-override` are only available with the `v2` backend. They will be ignored.";
170-
if context.is_json_output_enabled() {
171-
eprintln!(r#"{{"{JSON_CAUTION_KEY}":"{message}"}}"#);
172-
} else {
173-
eprintln!("{message}");
174-
// Add a blank line to separate this message from the one related to the fast bootstrap that comes next.
175-
eprintln!();
176-
}
177-
}
178170
}
179171

180172
impl ConfigSource for CardanoDbDownloadCommand {

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,65 @@ impl CardanoDbSnapshotCommands {
7474
}
7575
}
7676
}
77+
78+
/// Print in stderr that the given parameters are not available with the v1 backend and will be ignored
79+
pub fn warn_unused_parameter_with_v1_backend<const N: usize>(
80+
context: &CommandContext,
81+
v2_only_parameters: [&str; N],
82+
) {
83+
use crate::utils::JSON_CAUTION_KEY;
84+
85+
let message = format_unused_parameter_with_v1_backend(v2_only_parameters);
86+
if context.is_json_output_enabled() {
87+
eprintln!(r#"{{"{JSON_CAUTION_KEY}":"{message}"}}"#);
88+
} else {
89+
eprintln!("{message}");
90+
// Add a blank line to separate this message from the one related to the fast bootstrap that comes next.
91+
eprintln!();
92+
}
93+
}
94+
95+
fn format_unused_parameter_with_v1_backend<const N: usize>(
96+
v2_only_parameters: [&str; N],
97+
) -> String {
98+
match v2_only_parameters.len() {
99+
0 => String::new(),
100+
1 => format!(
101+
"`{}` is only available with the `v2` backend. It will be ignored.",
102+
v2_only_parameters[0]
103+
),
104+
n => {
105+
format!(
106+
"`{}`, and `{}` are only available with the `v2` backend. They will be ignored.",
107+
v2_only_parameters[..n - 1].join("`, `"),
108+
v2_only_parameters[n - 1]
109+
)
110+
}
111+
}
112+
}
113+
114+
#[cfg(test)]
115+
mod tests {
116+
use super::*;
117+
118+
#[test]
119+
fn test_format_unused_parameter_with_v1_backend() {
120+
assert_eq!(format_unused_parameter_with_v1_backend([]), String::new());
121+
122+
assert_eq!(
123+
format_unused_parameter_with_v1_backend(["--test"]),
124+
"`--test` is only available with the `v2` backend. It will be ignored.".to_string()
125+
);
126+
127+
assert_eq!(
128+
format_unused_parameter_with_v1_backend(["--foo", "--bar"]),
129+
"`--foo`, and `--bar` are only available with the `v2` backend. They will be ignored."
130+
.to_string()
131+
);
132+
133+
assert_eq!(
134+
format_unused_parameter_with_v1_backend(["--test", "--foo", "--bar"]),
135+
"`--test`, `--foo`, and `--bar` are only available with the `v2` backend. They will be ignored.".to_string()
136+
);
137+
}
138+
}

0 commit comments

Comments
 (0)