-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
78 lines (66 loc) · 1.83 KB
/
main.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
use super::{manage_signers::ManageSigners, manage_sources::ManageSources};
use clap::{
builder::{OsStr, Resettable},
Args, Parser, Subcommand,
};
use std::{env, path::PathBuf};
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// The path to the configuration file.
#[arg(
short,
long,
value_name = "FILE",
env = "HANKO_CONFIG",
default_value = default_config_path()
)]
pub config: PathBuf,
#[command(flatten)]
logging: Logging,
#[command(subcommand)]
command: Commands,
}
#[derive(Args)]
#[group(multiple = false)]
struct Logging {
/// Enable verbose logging.
#[arg(short, long)]
verbose: bool,
/// Disable all output.
#[arg(long)]
silent: bool,
}
#[derive(Subcommand)]
enum Commands {
/// Update the allowed signers file.
Update,
/// Manage signers.
#[command(subcommand)]
Signer(ManageSigners),
/// Manage sources.
#[command(subcommand)]
Source(ManageSources),
}
/// The default configuration file path according to the XDG Base Directory Specification.
/// If neither `$XDG_CONFIG_HOME` nor `$HOME` are set, `Resettable::Reset` is returned, forcing the user to specify the path.
fn default_config_path() -> Resettable<OsStr> {
let dirname = env!("CARGO_PKG_NAME");
let filename = "config.toml";
if let Ok(xdg_config_home) = env::var("XDG_CONFIG_HOME") {
Resettable::Value(format!("{}/{}/{}", xdg_config_home, dirname, filename).into())
} else if let Ok(home) = env::var("HOME") {
Resettable::Value(format!("{}/.config/{}/{}", home, dirname, filename).into())
} else {
Resettable::Reset
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}
}