-
Notifications
You must be signed in to change notification settings - Fork 24
/
kickstart.rs
81 lines (68 loc) · 2.25 KB
/
kickstart.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
use std::error::Error;
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use kickstart::generation::Template;
use kickstart::terminal;
use kickstart::validate::validate_file;
#[derive(Parser)]
#[clap(version, author, about, subcommand_negates_reqs = true)]
pub struct Cli {
/// Template to use: a local path or a HTTP url pointing to a Git repository
#[clap(required = true)]
pub template: Option<String>,
/// Where to output the project: defaults to the current directory
#[clap(short = 'o', long, default_value = ".")]
pub output_dir: PathBuf,
/// A subdirectory of the chosen template to use, to allow nested templates.
#[clap(short = 's', long)]
pub sub_dir: Option<String>,
/// Do not prompt for parameters and only use the defaults from template.toml
#[clap(long, default_value_t = false)]
pub no_input: bool,
#[clap(subcommand)]
pub command: Option<Command>,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Validates that a template.toml is valid
Validate {
/// The path to the template.toml
path: PathBuf,
},
}
fn bail(e: &dyn Error) -> ! {
terminal::error(&format!("Error: {}", e));
let mut cause = e.source();
while let Some(e) = cause {
terminal::error(&format!("Reason: {}", e));
cause = e.source();
}
::std::process::exit(1)
}
fn main() {
let cli = Cli::parse();
if let Some(Command::Validate { path }) = cli.command {
let errs = match validate_file(path) {
Ok(e) => e,
Err(e) => bail(&e),
};
if !errs.is_empty() {
terminal::error("The template.toml is invalid:\n");
for err in errs {
terminal::error(&format!("- {}\n", err));
}
::std::process::exit(1);
} else {
terminal::success("The template.toml file is valid!\n");
}
} else {
let template = match Template::from_input(&cli.template.unwrap(), cli.sub_dir.as_deref()) {
Ok(t) => t,
Err(e) => bail(&e),
};
match template.generate(&cli.output_dir, cli.no_input) {
Ok(_) => terminal::success("\nEverything done, ready to go!\n"),
Err(e) => bail(&e),
};
}
}