nethsm_cli/
cli.rs

1//! Command line handling for the "nethsm" executable.
2
3use std::path::PathBuf;
4
5use clap::Parser;
6pub use config::{ConfigCommand, ConfigGetCommand, ConfigSetCommand};
7pub use env::{EnvAddCommand, EnvCommand, EnvDeleteCommand};
8pub use health::HealthCommand;
9pub use info::InfoCommand;
10pub use key::{KeyCertCommand, KeyCommand};
11pub use lock::LockCommand;
12pub use metrics::MetricsCommand;
13pub use namespace::NamespaceCommand;
14use nethsm::UserId;
15pub use openpgp::OpenPgpCommand;
16pub use provision::ProvisionCommand;
17pub use random::RandomCommand;
18pub use system::SystemCommand;
19pub use unlock::UnlockCommand;
20pub use user::UserCommand;
21
22use crate::passphrase_file::PassphraseFile;
23
24mod config;
25mod env;
26mod health;
27mod info;
28mod key;
29mod lock;
30mod metrics;
31mod namespace;
32mod openpgp;
33mod provision;
34mod random;
35mod system;
36mod unlock;
37mod user;
38
39/// The name of the executable.
40const BIN_NAME: &str = "nethsm";
41
42/// Errors related to the CLI
43#[derive(Debug, thiserror::Error)]
44pub enum Error {
45    /// An option is missing
46    #[error(
47        "The \"{0}\" option must be provided for this command if more than one environment is defined."
48    )]
49    OptionMissing(String),
50}
51
52/// The "nethsm" CLI.
53#[derive(Debug, Parser)]
54#[command(name = BIN_NAME)]
55pub struct Cli {
56    /// Passphrase file(s).
57    #[arg(
58        env = "NETHSM_AUTH_PASSPHRASE_FILE",
59        global = true,
60        help = "The path to a file containing a passphrase for authentication",
61        long_help = "The path to a file containing a passphrase for authentication
62
63The passphrase provided in the file must be the one for the user chosen for the command.
64
65This option can be provided multiple times, which is needed for commands that require multiple roles at once.
66With multiple passphrase files ordering matters, as the files are assigned to the respective user provided by the \"--user\" option.",
67        long,
68        short
69    )]
70    pub auth_passphrase_file: Vec<PassphraseFile>,
71
72    /// An optional config file to use.
73    #[arg(
74        env = "NETHSM_CONFIG",
75        global = true,
76        help = "The path to a custom configuration file",
77        long_help = "The path to a custom configuration file
78
79If specified, the custom configuration file is used instead of the default configuration file location.",
80        long,
81        short
82    )]
83    pub config: Option<PathBuf>,
84
85    /// An optional label to use for a NetHSM backend.
86    #[arg(
87        env = "NETHSM_LABEL",
88        global = true,
89        help = "A label uniquely identifying a device in the configuration file",
90        long_help = "A label uniquely identifying a device in the configuration file
91
92Must be provided if more than one device is setup in the configuration file.",
93        long,
94        short
95    )]
96    pub label: Option<String>,
97
98    /// A user ID to use with a NetHSM backend.
99    #[arg(
100        env = "NETHSM_USER",
101        global = true,
102        help = "A user name which is used for the command",
103        long_help = "A user name which is used for a command
104
105Can be provided, if no user name is setup in the configuration file for a device.
106Must be provided, if several user names of the same target role are setup in the configuration file for a device.
107
108This option can be provided multiple times, which is needed for commands that require multiple roles at once.
109",
110        long,
111        short
112    )]
113    pub user: Vec<UserId>,
114
115    /// A "nethsm" subcommand.
116    #[command(subcommand)]
117    pub command: Command,
118}
119
120/// The "nethsm" CLI commands.
121#[derive(Debug, Parser)]
122#[command(about, author, version)]
123pub enum Command {
124    /// The "nethsm config" command.
125    #[command(subcommand)]
126    Config(ConfigCommand),
127
128    /// The "nethsm env" command.
129    #[command(subcommand)]
130    Env(EnvCommand),
131
132    /// The "nethsm health" command.
133    #[command(subcommand)]
134    Health(HealthCommand),
135
136    /// The "nethsm info" command.
137    Info(InfoCommand),
138
139    /// The "nethsm key" command.
140    #[command(subcommand)]
141    Key(KeyCommand),
142
143    /// The "nethsm lock" command.
144    Lock(LockCommand),
145
146    /// The "nethsm metrics" command.
147    Metrics(MetricsCommand),
148
149    /// The "nethsm namespace" command.
150    #[command(subcommand)]
151    Namespace(NamespaceCommand),
152
153    /// The "nethsm openpgp" command.
154    #[command(subcommand, name = "openpgp")]
155    OpenPgp(OpenPgpCommand),
156
157    /// The "nethsm provision" command.
158    Provision(ProvisionCommand),
159
160    /// The "nethsm random" command.
161    Random(RandomCommand),
162
163    /// The "nethsm system" command.
164    #[command(subcommand)]
165    System(SystemCommand),
166
167    /// The "nethsm unlock" command.
168    Unlock(UnlockCommand),
169
170    /// The "nethsm user" command.
171    #[command(subcommand)]
172    User(UserCommand),
173}