Skip to content

Commit

Permalink
Use clap_derive for stratisd-tools subcommands
Browse files Browse the repository at this point in the history
Signed-off-by: the Mulhern <[email protected]>
  • Loading branch information
the Mulhern committed Jan 27, 2025
1 parent 806ffe0 commit c0bd02e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 91 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ features = ["clock", "std"]
[dependencies.clap]
version = "4.5.0"
optional = true
features = ["derive"]

[dependencies.crc]
version = "3.0.0"
Expand Down
151 changes: 60 additions & 91 deletions src/bin/tools/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,100 @@

use std::path::PathBuf;

use clap::{Arg, ArgAction, Command};
use clap::{ArgAction, Parser};

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

use stratisd::stratis::VERSION;

pub trait ToolCommand<'a> {
fn name(&self) -> &'a str;
fn run(&self, command_line_args: Vec<String>) -> Result<(), String>;
}

struct StratisDumpMetadata;

impl StratisDumpMetadata {
fn cmd() -> Command {
Command::new("stratis-dumpmetadata")
.version(VERSION)
.about("Reads Stratis metadata from a Stratis device and displays it")
.next_line_help(true)
.arg(
Arg::new("dev")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
.help("Print metadata of given device"),
)
.arg(
Arg::new("print_bytes")
.long("print-bytes")
.action(ArgAction::SetTrue)
.num_args(0)
.short('b')
.help("Print byte buffer of signature block"),
)
.arg(
Arg::new("only")
.long("only")
.action(ArgAction::Set)
.value_name("PORTION")
.value_parser(["pool"])
.help("Only print specified portion of the metadata"),
)
}
#[derive(Parser)]
#[command(
version,
name = "stratis-dumpmetadata",
about = "Reads Stratis metadata from a Stratis device and displays it",
next_line_help = true
)]
struct StratisDumpMetadataCli {
/// Print metadata of given device
#[arg(required = true)]
dev: PathBuf,

#[arg(action = ArgAction::SetTrue, help="Print byte buffer of signature block", long="print-bytes", num_args=0, short='b')]
print_bytes: bool,

#[arg(action = ArgAction::Set, help="Only print specified portion of the metadata", long="only", value_name = "PORTION", value_parser=["pool"])]
only: Option<String>,
}

struct StratisDumpMetadata;

impl<'a> ToolCommand<'a> for StratisDumpMetadata {
fn name(&self) -> &'a str {
"stratis-dumpmetadata"
}

fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
let matches = StratisDumpMetadata::cmd().get_matches_from(command_line_args);
let devpath = matches
.get_one::<PathBuf>("dev")
.expect("'dev' is a mandatory argument");

let matches = StratisDumpMetadataCli::parse_from(command_line_args);
dump_metadata::run(
devpath,
matches.get_flag("print_bytes"),
matches
.get_one::<String>("only")
.map(|v| v == "pool")
.unwrap_or(false),
&matches.dev,
matches.print_bytes,
matches.only.map(|v| v == "pool").unwrap_or(false),
)
}
}

struct StratisCheckMetadata;

impl StratisCheckMetadata {
fn cmd() -> Command {
Command::new("stratis-checkmetadata")
.version(VERSION)
.about("Check validity of Stratis metadata")
.next_line_help(true)
.arg(
Arg::new("file")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
.help("File containing pool-level metadata as JSON"),
)
}
#[derive(Parser)]
#[command(
version,
name = "stratis-checkmetadata",
about = "Check validity of Stratis metadata",
next_line_help = true
)]
struct StratisCheckMetadataCli {
/// File containing pool-level metadata as JSON
#[arg(required = true)]
file: PathBuf,
}

struct StratisCheckMetadata;

impl<'a> ToolCommand<'a> for StratisCheckMetadata {
fn name(&self) -> &'a str {
"stratis-checkmetadata"
}

fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
let matches = StratisCheckMetadata::cmd().get_matches_from(command_line_args);
let infile = matches
.get_one::<PathBuf>("file")
.expect("'file' is a mandatory argument");

check_metadata::run(infile, false)
let matches = StratisCheckMetadataCli::parse_from(command_line_args);
check_metadata::run(&matches.file, false)
}
}

struct StratisPrintMetadata;

impl StratisPrintMetadata {
fn cmd() -> Command {
Command::new("stratis-printmetadata")
.version(VERSION)
.about("Print a human-suitable representation of Stratis metadata")
.next_line_help(true)
.arg(
Arg::new("file")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
.help("File containing pool-level metadata as JSON"),
)
}
#[derive(Parser)]
#[command(
version,
name = "stratis-printmetadata",
about = "Print a human-suitable representation of Stratis metadata",
next_line_help = true
)]
struct StratisPrintMetadataCli {
/// File containing pool-level metadata as JSON
#[arg(required = true)]
file: PathBuf,
}

struct StratisPrintMetadata;

impl<'a> ToolCommand<'a> for StratisPrintMetadata {
fn name(&self) -> &'a str {
"stratis-printmetadata"
}

fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
let matches = StratisPrintMetadata::cmd().get_matches_from(command_line_args);
let infile = matches
.get_one::<PathBuf>("file")
.expect("'file' is a mandatory argument");

check_metadata::run(infile, true)
let matches = StratisPrintMetadataCli::parse_from(command_line_args);
check_metadata::run(&matches.file, true)
}
}

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

use super::{StratisCheckMetadata, StratisDumpMetadata, StratisPrintMetadata};
use clap::CommandFactory;

use super::{StratisCheckMetadataCli, StratisDumpMetadataCli, StratisPrintMetadataCli};

#[test]
fn test_dumpmetadata_parse_args() {
StratisCheckMetadata::cmd().debug_assert();
StratisDumpMetadata::cmd().debug_assert();
StratisPrintMetadata::cmd().debug_assert();
StratisCheckMetadataCli::command().debug_assert();
StratisDumpMetadataCli::command().debug_assert();
StratisPrintMetadataCli::command().debug_assert();
}
}

0 comments on commit c0bd02e

Please sign in to comment.