mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-23 00:00:02 +03:00
refactor: reduce UnlockPolicy
boilerplate
Signed-off-by: Thomas Mühlbacher <tmuehlbacher@posteo.net>
This commit is contained in:
parent
de6e8ac230
commit
21b1111b59
30
Cargo.lock
generated
30
Cargo.lock
generated
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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,
|
||||
|
55
src/key.rs
55
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<Self> {
|
||||
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<PossibleValue> {
|
||||
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")),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user