1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! The KMS `yubihsm` subcommand

mod detect;
mod keys;
mod setup;
mod test;

pub use self::{detect::DetectCommand, keys::KeysCommand, setup::SetupCommand, test::TestCommand};
use abscissa::{Command, Help, Runnable};

/// The `yubihsm` subcommand
#[derive(Command, Debug, Options, Runnable)]
pub enum YubihsmCommand {
    /// Detected connected YubiHSM2 devices
    #[options(help = "detect all YubiHSM2 devices connected via USB")]
    Detect(DetectCommand),

    /// Show help for the `yubihsm` subcommand
    #[options(help = "show help for the 'yubihsm' subcommand")]
    Help(Help<Self>),

    /// Key management subcommands
    #[options(help = "key management subcommands")]
    Keys(KeysCommand),

    /// Perform initial YubiHSM2 device setup
    #[options(help = "initial device setup and configuration")]
    Setup(SetupCommand),

    /// Perform a signing test
    #[options(help = "perform a signing test")]
    Test(TestCommand),
}

impl YubihsmCommand {
    pub(super) fn config_path(&self) -> Option<&String> {
        // Mark that we're invoking a `tmkms yubihsm` command
        crate::yubihsm::mark_cli_command();

        match self {
            YubihsmCommand::Keys(keys) => keys.config_path(),
            YubihsmCommand::Setup(setup) => setup.config.as_ref(),
            YubihsmCommand::Test(test) => test.config.as_ref(),
            _ => None,
        }
    }

    pub(super) fn verbose(&self) -> bool {
        match self {
            YubihsmCommand::Detect(detect) => detect.verbose,
            YubihsmCommand::Setup(setup) => setup.verbose,
            YubihsmCommand::Test(test) => test.verbose,
            _ => false,
        }
    }
}