2020-12-18 10:27:44 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-03-08 07:02:04 +01:00
|
|
|
use clap::{Arg, Command as ClapCommand, ArgMatches};
|
2020-12-18 10:27:44 +01:00
|
|
|
|
2022-01-08 10:20:36 +01:00
|
|
|
use crate::error::ColmenaError;
|
2020-12-29 06:35:43 +01:00
|
|
|
use crate::util;
|
2020-12-18 10:27:44 +01:00
|
|
|
|
2022-03-08 07:02:04 +01:00
|
|
|
pub fn subcommand() -> ClapCommand<'static> {
|
2022-01-03 19:37:03 +01:00
|
|
|
subcommand_gen("eval")
|
|
|
|
}
|
|
|
|
|
2022-03-08 07:02:04 +01:00
|
|
|
pub fn deprecated_alias() -> ClapCommand<'static> {
|
2022-01-03 19:37:03 +01:00
|
|
|
subcommand_gen("introspect")
|
2022-03-08 07:02:04 +01:00
|
|
|
.hide(true)
|
2022-01-03 19:37:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 07:02:04 +01:00
|
|
|
fn subcommand_gen(name: &str) -> ClapCommand<'static> {
|
|
|
|
ClapCommand::new(name)
|
2022-01-03 19:37:03 +01:00
|
|
|
.about("Evaluate an expression using the complete configuration")
|
|
|
|
.long_about(r#"Evaluate an expression using the complete configuration
|
2021-03-18 03:03:53 +01:00
|
|
|
|
|
|
|
Your expression should take an attribute set with keys `pkgs`, `lib` and `nodes` (like a NixOS module) and return a JSON-serializable value.
|
2020-12-18 10:27:44 +01:00
|
|
|
|
|
|
|
For example, to retrieve the configuration of one node, you may write something like:
|
|
|
|
|
|
|
|
{ nodes, ... }: nodes.node-a.config.networking.hostName
|
|
|
|
"#)
|
2022-01-03 19:37:03 +01:00
|
|
|
.arg(Arg::new("expression_file")
|
2020-12-18 10:27:44 +01:00
|
|
|
.index(1)
|
2021-03-18 03:03:53 +01:00
|
|
|
.value_name("FILE")
|
2020-12-18 10:27:44 +01:00
|
|
|
.help("The .nix file containing the expression")
|
|
|
|
.takes_value(true))
|
2022-01-03 19:37:03 +01:00
|
|
|
.arg(Arg::new("expression")
|
|
|
|
.short('E')
|
2021-03-18 03:03:53 +01:00
|
|
|
.value_name("EXPRESSION")
|
2020-12-18 10:27:44 +01:00
|
|
|
.help("The Nix expression")
|
|
|
|
.takes_value(true))
|
2022-01-03 19:37:03 +01:00
|
|
|
.arg(Arg::new("instantiate")
|
2021-10-28 23:09:35 +02:00
|
|
|
.long("instantiate")
|
|
|
|
.help("Actually instantiate the expression")
|
|
|
|
.takes_value(false))
|
2020-12-18 10:27:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 10:20:36 +01:00
|
|
|
pub async fn run(global_args: &ArgMatches, local_args: &ArgMatches) -> Result<(), ColmenaError> {
|
2021-10-29 02:27:30 +02:00
|
|
|
if let Some("introspect") = global_args.subcommand_name() {
|
|
|
|
log::warn!("`colmena introspect` has been renamed to `colmena eval`. Please update your scripts.");
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:15:20 +01:00
|
|
|
let hive = util::hive_from_args(local_args).await?;
|
2020-12-18 10:27:44 +01:00
|
|
|
|
|
|
|
if !(local_args.is_present("expression") ^ local_args.is_present("expression_file")) {
|
2021-03-18 03:03:53 +01:00
|
|
|
log::error!("Either an expression (-E) or a .nix file containing an expression should be specified, not both.");
|
2020-12-18 10:27:44 +01:00
|
|
|
quit::with_code(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let expression = if local_args.is_present("expression") {
|
|
|
|
local_args.value_of("expression").unwrap().to_string()
|
|
|
|
} else {
|
|
|
|
let path: PathBuf = local_args.value_of("expression_file").unwrap().into();
|
|
|
|
format!("import {}", path.canonicalize().expect("Could not generate absolute path to expression file.").to_str().unwrap())
|
|
|
|
};
|
|
|
|
|
2021-10-28 23:09:35 +02:00
|
|
|
let instantiate = local_args.is_present("instantiate");
|
2021-11-18 22:15:20 +01:00
|
|
|
let result = hive.introspect(expression, instantiate).await?;
|
2021-10-28 23:09:35 +02:00
|
|
|
|
|
|
|
if instantiate {
|
|
|
|
print!("{}", result);
|
|
|
|
} else {
|
|
|
|
println!("{}", result);
|
|
|
|
}
|
2021-11-18 22:15:20 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-12-18 10:27:44 +01:00
|
|
|
}
|