Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clap_derive for stratisd-tools subcommands #3749

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
153 changes: 62 additions & 91 deletions src/bin/tools/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,102 @@

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,

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

/// Only print specified portion of the metadata
#[arg(action = ArgAction::Set, 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 +114,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();
}
}
Loading