Skip to content

Commit

Permalink
Specify PathBuf type for blockdevs in parser
Browse files Browse the repository at this point in the history
Note that this does virtually nothing. A PathBuf is basically just an
OsString with extra behaviors. And it can always be parsed from a
String, which is the clap default argument value type.

All it means is that when processing the argument values, the paths will
not have to be converted to Strings and then Pathbuf's because that
conversion happened when they were read.

It is really just good practice.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 8, 2024
1 parent 4dda0c3 commit df94534
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/bin/stratis-min/stratis-min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::{error::Error, path::PathBuf};

use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command};
use clap::{Arg, ArgAction, ArgGroup, Command};
use serde_json::{json, Map, Value};

use stratisd::{
Expand Down Expand Up @@ -56,6 +56,7 @@ fn parse_args() -> Command {
.arg(
Arg::new("blockdevs")
.action(ArgAction::Append)
.value_parser(clap::value_parser!(PathBuf))
.required(true),
)
.arg(Arg::new("key_desc").long("key-desc").num_args(1))
Expand Down Expand Up @@ -86,6 +87,7 @@ fn parse_args() -> Command {
.arg(
Arg::new("blockdevs")
.action(ArgAction::Append)
.value_parser(clap::value_parser!(PathBuf))
.required(true),
),
Command::new("rename")
Expand All @@ -96,13 +98,15 @@ fn parse_args() -> Command {
.arg(
Arg::new("blockdevs")
.action(ArgAction::Append)
.value_parser(clap::value_parser!(PathBuf))
.required(true),
),
Command::new("add-cache")
.arg(Arg::new("name").required(true))
.arg(
Arg::new("blockdevs")
.action(ArgAction::Append)
.value_parser(clap::value_parser!(PathBuf))
.required(true),
),
Command::new("destroy").arg(Arg::new("name").required(true)),
Expand Down Expand Up @@ -195,13 +199,6 @@ fn parse_args() -> Command {
])
}

fn get_paths_from_args(args: &ArgMatches) -> Vec<PathBuf> {
args.get_many::<String>("blockdevs")
.expect("required")
.map(PathBuf::from)
.collect::<Vec<_>>()
}

fn main() -> Result<(), String> {
fn main_box() -> Result<(), Box<dyn Error>> {
let cmd = parse_args();
Expand Down Expand Up @@ -267,7 +264,6 @@ fn main() -> Result<(), String> {
pool::pool_stop(id)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("create") {
let paths = get_paths_from_args(args);
let key_description = match args.get_one::<String>("key_desc").map(|s| s.to_owned())
{
Some(string) => Some(KeyDescription::try_from(string)?),
Expand Down Expand Up @@ -300,18 +296,23 @@ fn main() -> Result<(), String> {
};
pool::pool_create(
args.get_one::<String>("name").expect("required").to_owned(),
paths,
args.get_many::<PathBuf>("blockdevs")
.expect("required")
.cloned()
.collect::<Vec<_>>(),
EncryptionInfo::from_options((key_description, clevis_info)),
)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("destroy") {
pool::pool_destroy(args.get_one::<String>("name").expect("required").to_owned())?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("init-cache") {
let paths = get_paths_from_args(args);
pool::pool_init_cache(
args.get_one::<String>("name").expect("required").to_owned(),
paths,
args.get_many::<PathBuf>("blockdevs")
.expect("required")
.cloned()
.collect::<Vec<_>>(),
)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("rename") {
Expand All @@ -325,17 +326,21 @@ fn main() -> Result<(), String> {
)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("add-data") {
let paths = get_paths_from_args(args);
pool::pool_add_data(
args.get_one::<String>("name").expect("required").to_owned(),
paths,
args.get_many::<PathBuf>("blockdevs")
.expect("required")
.cloned()
.collect::<Vec<_>>(),
)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("add-cache") {
let paths = get_paths_from_args(args);
pool::pool_add_cache(
args.get_one::<String>("name").expect("required").to_owned(),
paths,
args.get_many::<PathBuf>("blockdevs")
.expect("required")
.cloned()
.collect::<Vec<_>>(),
)?;
Ok(())
} else if let Some(args) = subcommand.subcommand_matches("is-encrypted") {
Expand Down

0 comments on commit df94534

Please sign in to comment.