colmena/src/cli.rs

108 lines
3.7 KiB
Rust
Raw Normal View History

2021-03-23 22:14:04 +01:00
//! Global CLI Setup.
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
2021-03-23 22:14:04 +01:00
use crate::command;
macro_rules! register_command {
($module:ident, $app:ident) => {
$app = $app.subcommand(command::$module::subcommand());
};
}
macro_rules! handle_command {
($module:ident, $matches:ident) => {
if let Some(sub_matches) = $matches.subcommand_matches(stringify!($module)) {
command::$module::run(&$matches, &sub_matches).await;
return;
}
};
($name:expr, $module:ident, $matches:ident) => {
if let Some(sub_matches) = $matches.subcommand_matches($name) {
command::$module::run(&$matches, &sub_matches).await;
return;
}
};
}
pub fn build_cli(include_internal: bool) -> App<'static, 'static> {
2021-03-23 22:14:04 +01:00
let mut app = App::new("Colmena")
2021-03-23 22:14:04 +01:00
.bin_name("colmena")
2021-03-23 22:14:04 +01:00
.version("0.1.0")
.author("Zhaofeng Li <hello@zhaofeng.li>")
.about("NixOS deployment tool")
.global_setting(AppSettings::ColoredHelp)
.setting(AppSettings::ArgRequiredElseHelp)
.arg(Arg::with_name("config")
.short("f")
.long("config")
.value_name("CONFIG")
2021-06-29 10:19:13 +02:00
.help("Path to a Hive expression, a flake.nix, or a Nix Flake URI")
2021-03-23 22:14:04 +01:00
// The default value is a lie (sort of)!
//
// The default behavior is to search upwards from the
// current working directory for a file named "flake.nix"
// or "hive.nix". This behavior is disabled if --config/-f
// is explicitly supplied by the user (occurrences_of > 0).
2021-03-23 22:14:04 +01:00
.default_value("hive.nix")
.long_help(r#"If this argument is not specified, Colmena will search upwards from the current working directory for a file named "flake.nix" or "hive.nix". This behavior is disabled if --config/-f is given explicitly.
2021-03-23 22:14:04 +01:00
For a sample configuration, see <https://github.com/zhaofengli/colmena>.
"#)
.global(true))
.arg(Arg::with_name("show-trace")
.long("show-trace")
.help("Show debug information for Nix commands")
.long_help("Passes --show-trace to Nix commands")
.global(true)
.takes_value(false));
if include_internal {
app = app.subcommand(SubCommand::with_name("gen-completions")
.about("Generate shell auto-completion files (Internal)")
.setting(AppSettings::Hidden)
.arg(Arg::with_name("shell")
.index(1)
.required(true)
.takes_value(true)));
}
2021-03-23 22:14:04 +01:00
register_command!(apply, app);
register_command!(apply_local, app);
register_command!(build, app);
register_command!(introspect, app);
register_command!(upload_keys, app);
register_command!(exec, app);
2021-06-29 10:02:43 +02:00
register_command!(nix_info, app);
2021-03-23 22:14:04 +01:00
app
}
pub async fn run() {
let mut app = build_cli(true);
2021-03-23 22:14:04 +01:00
let matches = app.clone().get_matches();
handle_command!(apply, matches);
handle_command!("apply-local", apply_local, matches);
handle_command!(build, matches);
handle_command!(introspect, matches);
handle_command!("upload-keys", upload_keys, matches);
handle_command!(exec, matches);
2021-06-29 10:02:43 +02:00
handle_command!("nix-info", nix_info, matches);
2021-03-23 22:14:04 +01:00
if let Some(args) = matches.subcommand_matches("gen-completions") {
return gen_completions(args);
}
2021-03-23 22:14:04 +01:00
app.print_long_help().unwrap();
println!();
}
fn gen_completions(args: &ArgMatches<'_>) {
let mut app = build_cli(false);
let shell: clap::Shell = args.value_of("shell").unwrap()
.parse().unwrap();
app.gen_completions_to("colmena", shell, &mut std::io::stdout());
}