diff --git a/Cargo.lock b/Cargo.lock index 22ac0e3b..61e55c83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,8 @@ dependencies = [ "libc", "log", "rpassword", + "strum", + "strum_macros", "udev", "uuid", ] @@ -516,6 +518,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + [[package]] name = "shlex" version = "1.3.0" @@ -528,6 +536,28 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" version = "2.0.48" diff --git a/Cargo.toml b/Cargo.toml index 853123ee..8e762952 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,5 @@ either = "1.5" rpassword = "7" bch_bindgen = { path = "bch_bindgen" } byteorder = "1.3" +strum = { version = "0.26", features = ["derive"] } +strum_macros = "0.26" diff --git a/src/commands/mount.rs b/src/commands/mount.rs index 474f58ff..7f5937d0 100644 --- a/src/commands/mount.rs +++ b/src/commands/mount.rs @@ -231,7 +231,8 @@ pub struct Cli { #[arg( short = 'k', long = "key_location", - default_value = "ask", + value_enum, + default_value_t, verbatim_doc_comment )] unlock_policy: UnlockPolicy, diff --git a/src/key.rs b/src/key.rs index b8ddcd02..284d1844 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1,63 +1,25 @@ use std::{ - fmt, fs, + fmt::Debug, + fs, io::{stdin, IsTerminal}, }; -use crate::c_str; use anyhow::anyhow; use bch_bindgen::bcachefs::bch_sb_handle; -use clap::builder::PossibleValue; use log::info; -#[derive(Clone, Debug)] +use crate::c_str; + +#[derive(Clone, Debug, clap::ValueEnum, strum::Display)] pub enum UnlockPolicy { - None, Fail, Wait, Ask, } -impl std::str::FromStr for UnlockPolicy { - type Err = anyhow::Error; - fn from_str(s: &str) -> anyhow::Result { - match s { - "" | "none" => Ok(UnlockPolicy::None), - "fail" => Ok(UnlockPolicy::Fail), - "wait" => Ok(UnlockPolicy::Wait), - "ask" => Ok(UnlockPolicy::Ask), - _ => Err(anyhow!("Invalid unlock policy provided")), - } - } -} - -impl clap::ValueEnum for UnlockPolicy { - fn value_variants<'a>() -> &'a [Self] { - &[ - UnlockPolicy::None, - UnlockPolicy::Fail, - UnlockPolicy::Wait, - UnlockPolicy::Ask, - ] - } - - fn to_possible_value(&self) -> Option { - Some(match self { - Self::None => PossibleValue::new("none").alias(""), - Self::Fail => PossibleValue::new("fail"), - Self::Wait => PossibleValue::new("wait"), - Self::Ask => PossibleValue::new("ask"), - }) - } -} - -impl fmt::Display for UnlockPolicy { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - UnlockPolicy::None => write!(f, "None"), - UnlockPolicy::Fail => write!(f, "Fail"), - UnlockPolicy::Wait => write!(f, "Wait"), - UnlockPolicy::Ask => write!(f, "Ask"), - } +impl Default for UnlockPolicy { + fn default() -> Self { + Self::Ask } } @@ -185,6 +147,5 @@ pub fn apply_key_unlocking_policy( UnlockPolicy::Fail => Err(anyhow!("no passphrase available")), UnlockPolicy::Wait => Ok(wait_for_unlock(&block_device.sb().uuid())?), UnlockPolicy::Ask => ask_for_passphrase(block_device), - _ => Err(anyhow!("no unlock policy specified for locked filesystem")), } }