diff --git a/src/bin/stratis-min/stratis-min.rs b/src/bin/stratis-min/stratis-min.rs index 7a00f954bd..26aea85992 100644 --- a/src/bin/stratis-min/stratis-min.rs +++ b/src/bin/stratis-min/stratis-min.rs @@ -4,17 +4,15 @@ use std::{error::Error, path::PathBuf}; -use clap::{builder::PossibleValuesParser, Arg, ArgAction, ArgGroup, Command}; +use clap::{Arg, ArgAction, ArgGroup, Command}; use serde_json::{json, Map, Value}; -use strum::VariantNames; use stratisd::{ engine::{ - EncryptionInfo, KeyDescription, Name, PoolIdentifier, PoolUuid, UnlockMethod, - CLEVIS_TANG_TRUST_URL, + EncryptionInfo, KeyDescription, Name, PoolIdentifier, PoolUuid, CLEVIS_TANG_TRUST_URL, }, jsonrpc::client::{filesystem, key, pool, report}, - stratis::VERSION, + stratis::{UnlockMethod, VERSION}, }; fn parse_args() -> Command { @@ -46,7 +44,7 @@ fn parse_args() -> Command { Arg::new("unlock_method") .long("unlock-method") .num_args(1) - .value_parser(PossibleValuesParser::new(UnlockMethod::VARIANTS)), + .value_parser(clap::value_parser!(UnlockMethod)), ) .arg( Arg::new("prompt") @@ -241,9 +239,7 @@ fn main() -> Result<(), String> { .expect("required"), )?) }; - let unlock_method = args.get_one::("unlock_method").map(|s| { - UnlockMethod::try_from(s.as_str()).expect("parser ensures valid string value") - }); + let unlock_method = args.get_one::("unlock_method").copied(); let prompt = args.get_flag("prompt"); pool::pool_start(id, unlock_method, prompt)?; Ok(()) diff --git a/src/engine/types/mod.rs b/src/engine/types/mod.rs index 80e1f93e54..14d1bfa48a 100644 --- a/src/engine/types/mod.rs +++ b/src/engine/types/mod.rs @@ -16,7 +16,9 @@ use std::{ use libudev::EventType; use serde::{Deserialize, Serialize}; use serde_json::Value; -use strum_macros::{self, AsRefStr, EnumString, FromRepr, VariantNames}; +use strum_macros::{ + self, AsRefStr, EnumString, FromRepr, IntoStaticStr, VariantArray, VariantNames, +}; use uuid::Uuid; use devicemapper::{Bytes, Sectors, IEC}; @@ -137,7 +139,18 @@ impl Display for StratisUuid { } /// Use Clevis or keyring to unlock LUKS volume. -#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, Debug, EnumString, VariantNames)] +#[derive( + Serialize, + Deserialize, + Clone, + Copy, + Eq, + PartialEq, + Debug, + EnumString, + VariantArray, + IntoStaticStr, +)] #[strum(serialize_all = "snake_case")] pub enum UnlockMethod { Clevis, diff --git a/src/stratis/command_line.rs b/src/stratis/command_line.rs new file mode 100644 index 0000000000..ff15124026 --- /dev/null +++ b/src/stratis/command_line.rs @@ -0,0 +1,20 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use clap::{builder::PossibleValue, ValueEnum}; + +use strum::VariantArray; + +pub use crate::engine::UnlockMethod; + +impl ValueEnum for UnlockMethod { + fn value_variants<'a>() -> &'a [UnlockMethod] { + UnlockMethod::VARIANTS + } + + fn to_possible_value(&self) -> Option { + let value: &'static str = self.into(); + Some(PossibleValue::new(value)) + } +} diff --git a/src/stratis/mod.rs b/src/stratis/mod.rs index 06053b75dc..2c200b92b8 100644 --- a/src/stratis/mod.rs +++ b/src/stratis/mod.rs @@ -3,11 +3,13 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. pub use self::{ + command_line::UnlockMethod, errors::{StratisError, StratisResult}, run::run, stratis::VERSION, }; +mod command_line; mod dm; mod errors; mod ipc_support;