nethsm_cli/
cli.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::path::PathBuf;

use clap::Parser;
pub use config::{ConfigCommand, ConfigGetCommand, ConfigSetCommand};
pub use env::{EnvAddCommand, EnvCommand, EnvDeleteCommand};
pub use health::HealthCommand;
pub use info::InfoCommand;
pub use key::{KeyCertCommand, KeyCommand};
pub use lock::LockCommand;
pub use metrics::MetricsCommand;
pub use namespace::NamespaceCommand;
use nethsm::UserId;
pub use openpgp::OpenPgpCommand;
pub use provision::ProvisionCommand;
pub use random::RandomCommand;
pub use system::SystemCommand;
pub use unlock::UnlockCommand;
pub use user::UserCommand;

use crate::passphrase_file::PassphraseFile;

mod config;
mod env;
mod health;
mod info;
mod key;
mod lock;
mod metrics;
mod namespace;
mod openpgp;
mod provision;
mod random;
mod system;
mod unlock;
mod user;

pub const BIN_NAME: &str = "nethsm";

/// Errors related to the CLI
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// An option is missing
    #[error("The \"{0}\" option must be provided for this command if more than one environment is defined.")]
    OptionMissing(String),
}

#[derive(Debug, Parser)]
#[command(name = BIN_NAME)]
pub struct Cli {
    #[arg(
        env = "NETHSM_AUTH_PASSPHRASE_FILE",
        global = true,
        help = "The path to a file containing a passphrase for authentication",
        long_help = "The path to a file containing a passphrase for authentication

The passphrase provided in the file must be the one for the user chosen for the command.

This option can be provided multiple times, which is needed for commands that require multiple roles at once.
With multiple passphrase files ordering matters, as the files are assigned to the respective user provided by the \"--user\" option.",
        long,
        short
    )]
    pub auth_passphrase_file: Vec<PassphraseFile>,

    #[arg(
        env = "NETHSM_CONFIG",
        global = true,
        help = "The path to a custom configuration file",
        long_help = "The path to a custom configuration file

If specified, the custom configuration file is used instead of the default configuration file location.",
        long,
        short
    )]
    pub config: Option<PathBuf>,

    #[arg(
        env = "NETHSM_LABEL",
        global = true,
        help = "A label uniquely identifying a device in the configuration file",
        long_help = "A label uniquely identifying a device in the configuration file

Must be provided if more than one device is setup in the configuration file.",
        long,
        short
    )]
    pub label: Option<String>,

    #[arg(
        env = "NETHSM_USER",
        global = true,
        help = "A user name which is used for the command",
        long_help = "A user name which is used for a command

Can be provided, if no user name is setup in the configuration file for a device.
Must be provided, if several user names of the same target role are setup in the configuration file for a device.

This option can be provided multiple times, which is needed for commands that require multiple roles at once.
",
        long,
        short
    )]
    pub user: Vec<UserId>,

    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Parser)]
#[command(about, author, version)]
pub enum Command {
    #[command(subcommand)]
    Config(ConfigCommand),

    #[command(subcommand)]
    Env(EnvCommand),

    #[command(subcommand)]
    Health(HealthCommand),

    Info(InfoCommand),

    #[command(subcommand)]
    Key(KeyCommand),

    Lock(LockCommand),

    Metrics(MetricsCommand),

    #[command(subcommand)]
    Namespace(NamespaceCommand),

    #[command(subcommand, name = "openpgp")]
    OpenPgp(OpenPgpCommand),

    Provision(ProvisionCommand),

    Random(RandomCommand),

    #[command(subcommand)]
    System(SystemCommand),

    Unlock(UnlockCommand),

    #[command(subcommand)]
    User(UserCommand),
}