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
mod generate;
mod start;
mod version;
use self::ZebradCmd::*;
use self::{generate::GenerateCmd, start::StartCmd, version::VersionCmd};
use crate::config::ZebradConfig;
use abscissa_core::{
config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable,
};
use std::path::PathBuf;
pub const CONFIG_FILE: &str = "zebrad.toml";
#[derive(Command, Debug, Options)]
pub enum ZebradCmd {
#[options(help = "generate a skeleton configuration")]
Generate(GenerateCmd),
#[options(help = "get usage information")]
Help(Help<Self>),
#[options(help = "start the application")]
Start(StartCmd),
#[options(help = "display version information")]
Version(VersionCmd),
}
impl ZebradCmd {
pub(crate) fn is_server(&self) -> bool {
match self {
Start(_) => true,
Generate(_) | Help(_) | Version(_) => false,
}
}
}
impl Runnable for ZebradCmd {
fn run(&self) {
match self {
Generate(cmd) => cmd.run(),
ZebradCmd::Help(cmd) => cmd.run(),
Start(cmd) => cmd.run(),
Version(cmd) => cmd.run(),
}
}
}
impl Configurable<ZebradConfig> for ZebradCmd {
fn config_path(&self) -> Option<PathBuf> {
let if_exists = |f: PathBuf| if f.exists() { Some(f) } else { None };
dirs::preference_dir()
.map(|path| path.join(CONFIG_FILE))
.and_then(if_exists)
}
fn process_config(&self, config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
match self {
ZebradCmd::Start(cmd) => cmd.override_config(config),
_ => Ok(config),
}
}
}