Skip to content

Commit 72d08ec

Browse files
author
the Mulhern
committed
Use clap_derive for stratisd-tools subcommands
Signed-off-by: the Mulhern <amulhern@amulhern.bos.csb>
1 parent 806ffe0 commit 72d08ec

3 files changed

Lines changed: 76 additions & 91 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ features = ["clock", "std"]
9292
[dependencies.clap]
9393
version = "4.5.0"
9494
optional = true
95+
features = ["derive"]
9596

9697
[dependencies.crc]
9798
version = "3.0.0"

src/bin/tools/cmds.rs

Lines changed: 62 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,133 +4,102 @@
44

55
use std::path::PathBuf;
66

7-
use clap::{Arg, ArgAction, Command};
7+
use clap::{ArgAction, Parser};
88

99
use crate::tools::{check_metadata, dump_metadata};
1010

11-
use stratisd::stratis::VERSION;
12-
1311
pub trait ToolCommand<'a> {
1412
fn name(&self) -> &'a str;
1513
fn run(&self, command_line_args: Vec<String>) -> Result<(), String>;
1614
}
1715

18-
struct StratisDumpMetadata;
19-
20-
impl StratisDumpMetadata {
21-
fn cmd() -> Command {
22-
Command::new("stratis-dumpmetadata")
23-
.version(VERSION)
24-
.about("Reads Stratis metadata from a Stratis device and displays it")
25-
.next_line_help(true)
26-
.arg(
27-
Arg::new("dev")
28-
.value_parser(clap::value_parser!(PathBuf))
29-
.required(true)
30-
.help("Print metadata of given device"),
31-
)
32-
.arg(
33-
Arg::new("print_bytes")
34-
.long("print-bytes")
35-
.action(ArgAction::SetTrue)
36-
.num_args(0)
37-
.short('b')
38-
.help("Print byte buffer of signature block"),
39-
)
40-
.arg(
41-
Arg::new("only")
42-
.long("only")
43-
.action(ArgAction::Set)
44-
.value_name("PORTION")
45-
.value_parser(["pool"])
46-
.help("Only print specified portion of the metadata"),
47-
)
48-
}
16+
#[derive(Parser)]
17+
#[command(
18+
version,
19+
name = "stratis-dumpmetadata",
20+
about = "Reads Stratis metadata from a Stratis device and displays it",
21+
next_line_help = true
22+
)]
23+
struct StratisDumpMetadataCli {
24+
/// Print metadata of given device
25+
#[arg(required = true)]
26+
dev: PathBuf,
27+
28+
/// Print byte buffer of signature block
29+
#[arg(action = ArgAction::SetTrue, long="print-bytes", num_args=0, short='b')]
30+
print_bytes: bool,
31+
32+
/// Only print specified portion of the metadata
33+
#[arg(action = ArgAction::Set, long="only", value_name = "PORTION", value_parser=["pool"])]
34+
only: Option<String>,
4935
}
5036

37+
struct StratisDumpMetadata;
38+
5139
impl<'a> ToolCommand<'a> for StratisDumpMetadata {
5240
fn name(&self) -> &'a str {
5341
"stratis-dumpmetadata"
5442
}
5543

5644
fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
57-
let matches = StratisDumpMetadata::cmd().get_matches_from(command_line_args);
58-
let devpath = matches
59-
.get_one::<PathBuf>("dev")
60-
.expect("'dev' is a mandatory argument");
61-
45+
let matches = StratisDumpMetadataCli::parse_from(command_line_args);
6246
dump_metadata::run(
63-
devpath,
64-
matches.get_flag("print_bytes"),
65-
matches
66-
.get_one::<String>("only")
67-
.map(|v| v == "pool")
68-
.unwrap_or(false),
47+
&matches.dev,
48+
matches.print_bytes,
49+
matches.only.map(|v| v == "pool").unwrap_or(false),
6950
)
7051
}
7152
}
7253

73-
struct StratisCheckMetadata;
74-
75-
impl StratisCheckMetadata {
76-
fn cmd() -> Command {
77-
Command::new("stratis-checkmetadata")
78-
.version(VERSION)
79-
.about("Check validity of Stratis metadata")
80-
.next_line_help(true)
81-
.arg(
82-
Arg::new("file")
83-
.value_parser(clap::value_parser!(PathBuf))
84-
.required(true)
85-
.help("File containing pool-level metadata as JSON"),
86-
)
87-
}
54+
#[derive(Parser)]
55+
#[command(
56+
version,
57+
name = "stratis-checkmetadata",
58+
about = "Check validity of Stratis metadata",
59+
next_line_help = true
60+
)]
61+
struct StratisCheckMetadataCli {
62+
/// File containing pool-level metadata as JSON
63+
#[arg(required = true)]
64+
file: PathBuf,
8865
}
8966

67+
struct StratisCheckMetadata;
68+
9069
impl<'a> ToolCommand<'a> for StratisCheckMetadata {
9170
fn name(&self) -> &'a str {
9271
"stratis-checkmetadata"
9372
}
9473

9574
fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
96-
let matches = StratisCheckMetadata::cmd().get_matches_from(command_line_args);
97-
let infile = matches
98-
.get_one::<PathBuf>("file")
99-
.expect("'file' is a mandatory argument");
100-
101-
check_metadata::run(infile, false)
75+
let matches = StratisCheckMetadataCli::parse_from(command_line_args);
76+
check_metadata::run(&matches.file, false)
10277
}
10378
}
10479

105-
struct StratisPrintMetadata;
106-
107-
impl StratisPrintMetadata {
108-
fn cmd() -> Command {
109-
Command::new("stratis-printmetadata")
110-
.version(VERSION)
111-
.about("Print a human-suitable representation of Stratis metadata")
112-
.next_line_help(true)
113-
.arg(
114-
Arg::new("file")
115-
.value_parser(clap::value_parser!(PathBuf))
116-
.required(true)
117-
.help("File containing pool-level metadata as JSON"),
118-
)
119-
}
80+
#[derive(Parser)]
81+
#[command(
82+
version,
83+
name = "stratis-printmetadata",
84+
about = "Print a human-suitable representation of Stratis metadata",
85+
next_line_help = true
86+
)]
87+
struct StratisPrintMetadataCli {
88+
/// File containing pool-level metadata as JSON
89+
#[arg(required = true)]
90+
file: PathBuf,
12091
}
12192

93+
struct StratisPrintMetadata;
94+
12295
impl<'a> ToolCommand<'a> for StratisPrintMetadata {
12396
fn name(&self) -> &'a str {
12497
"stratis-printmetadata"
12598
}
12699

127100
fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
128-
let matches = StratisPrintMetadata::cmd().get_matches_from(command_line_args);
129-
let infile = matches
130-
.get_one::<PathBuf>("file")
131-
.expect("'file' is a mandatory argument");
132-
133-
check_metadata::run(infile, true)
101+
let matches = StratisPrintMetadataCli::parse_from(command_line_args);
102+
check_metadata::run(&matches.file, true)
134103
}
135104
}
136105

@@ -145,12 +114,14 @@ pub fn cmds<'a>() -> Vec<Box<dyn ToolCommand<'a>>> {
145114
#[cfg(test)]
146115
mod tests {
147116

148-
use super::{StratisCheckMetadata, StratisDumpMetadata, StratisPrintMetadata};
117+
use clap::CommandFactory;
118+
119+
use super::{StratisCheckMetadataCli, StratisDumpMetadataCli, StratisPrintMetadataCli};
149120

150121
#[test]
151122
fn test_dumpmetadata_parse_args() {
152-
StratisCheckMetadata::cmd().debug_assert();
153-
StratisDumpMetadata::cmd().debug_assert();
154-
StratisPrintMetadata::cmd().debug_assert();
123+
StratisCheckMetadataCli::command().debug_assert();
124+
StratisDumpMetadataCli::command().debug_assert();
125+
StratisPrintMetadataCli::command().debug_assert();
155126
}
156127
}

0 commit comments

Comments
 (0)