Skip to content

Commit

Permalink
stratis-dumpmetadata: add struct based dispatch
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jan 9, 2024
1 parent 689c0d0 commit 0de5587
Showing 1 changed file with 73 additions and 42 deletions.
115 changes: 73 additions & 42 deletions src/bin/stratis-dumpmetadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,94 @@ fn initialize_log() {
builder.init()
}

fn parse_args() -> Command {
Command::new("stratis-dumpmetadata")
.next_line_help(true)
.arg(
Arg::new("dev")
.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"),
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")
.next_line_help(true)
.arg(
Arg::new("dev")
.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"),
)
}
}

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::<String>("dev")
.map(|s| s.as_str())
.expect("'dev' is a mandatory argument");

dump_metadata::run(
devpath,
matches.get_flag("print_bytes"),
matches
.get_one::<String>("only")
.map(|v| v == "pool")
.unwrap_or(false),
)
}
}

fn cmds<'a>() -> Vec<Box<dyn ToolCommand<'a>>> {
vec![Box::new(StratisDumpMetadata)]
}

fn main() {
let matches = parse_args().get_matches();
let devpath = matches
.get_one::<String>("dev")
.map(|s| s.as_str())
.expect("'dev' is a mandatory argument");
let args = env::args().collect::<Vec<_>>();
let argv1 = args[0].as_str();

initialize_log();

match dump_metadata::run(
devpath,
matches.get_flag("print_bytes"),
matches
.get_one::<String>("only")
.map(|v| v == "pool")
.unwrap_or(false),
) {
Ok(()) => {}
Err(e) => {
eprintln!("Error encountered: {}", e);
process::exit(1);
if let Some(c) = cmds().iter().find(|x| argv1.ends_with(x.name())) {
match c.run(args) {
Ok(()) => {}
Err(e) => {
eprintln!("Error encountered: {}", e);
process::exit(1);
}
}
} else {
process::exit(2);
}
}

#[cfg(test)]
mod tests {
use super::parse_args;

use super::StratisDumpMetadata;

#[test]
fn test_dumpmetadata_parse_args() {
parse_args().debug_assert();
StratisDumpMetadata::cmd().debug_assert();
}
}

0 comments on commit 0de5587

Please sign in to comment.