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
|
use anyhow::Context;
use directories_next::ProjectDirs;
use log::debug;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use vmadm::cmd;
use vmadm::config::Configuration;
use vmadm::spec::Specification;
const QUALIFIER: &str = "";
const ORG: &str = "";
const APP: &str = "vmadm";
#[derive(StructOpt, Debug)]
struct Cli {
#[structopt(subcommand)]
cmd: Command,
}
#[derive(StructOpt, Debug)]
enum Command {
New {
#[structopt(flatten)]
common: CommonOptions,
#[structopt(parse(from_os_str))]
spec: PathBuf,
},
List {
#[structopt(flatten)]
common: CommonOptions,
},
Delete {
#[structopt(flatten)]
common: CommonOptions,
#[structopt(parse(from_os_str))]
spec: PathBuf,
},
CloudInit {
#[structopt(flatten)]
common: CommonOptions,
#[structopt(parse(from_os_str))]
spec: PathBuf,
#[structopt(parse(from_os_str))]
dirname: PathBuf,
},
}
#[derive(StructOpt, Debug)]
struct CommonOptions {
#[structopt(short, long, parse(from_os_str))]
config: Option<PathBuf>,
}
fn main() -> anyhow::Result<()> {
pretty_env_logger::init_custom_env("VMADM_LOG");
let cli = Cli::from_args();
debug!("{:#?}", cli);
match cli.cmd {
Command::New { common, spec } => {
let specs = get_specs(&common, &spec)?;
cmd::new(&specs)?;
}
Command::List { common } => {
let config = config(&common)?;
cmd::list(&config)?;
}
Command::Delete { common, spec } => {
let specs = get_specs(&common, &spec)?;
cmd::delete(&specs)?;
}
Command::CloudInit {
common,
spec,
dirname,
} => {
let specs = get_specs(&common, &spec)?;
cmd::cloud_init(&specs, &dirname)?;
}
}
Ok(())
}
fn get_specs(common: &CommonOptions, spec: &Path) -> anyhow::Result<Vec<Specification>> {
let config = config(&common)?;
let specs = Specification::from_file(&config, &spec)?;
Ok(specs)
}
fn config(common: &CommonOptions) -> anyhow::Result<Configuration> {
let filename = config_filename(common);
let config = Configuration::from_file(&filename)
.with_context(|| format!("reading configuration file {}", filename.display()))?;
Ok(config)
}
fn config_filename(common: &CommonOptions) -> PathBuf {
if let Some(ref filename) = common.config {
filename.to_path_buf()
} else {
if let Some(dirs) = ProjectDirs::from(QUALIFIER, ORG, APP) {
dirs.config_dir().join("config.yaml")
} else {
PathBuf::from("xxx")
}
}
}
|