introspect: Support actually instantiating the expression

This commit is contained in:
Zhaofeng Li 2021-10-28 14:09:35 -07:00
parent fac91c524b
commit 765f42fa24
2 changed files with 22 additions and 6 deletions

View file

@ -25,6 +25,10 @@ For example, to retrieve the configuration of one node, you may write something
.value_name("EXPRESSION") .value_name("EXPRESSION")
.help("The Nix expression") .help("The Nix expression")
.takes_value(true)) .takes_value(true))
.arg(Arg::with_name("instantiate")
.long("instantiate")
.help("Actually instantiate the expression")
.takes_value(false))
} }
pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) { pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
@ -42,6 +46,12 @@ pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
format!("import {}", path.canonicalize().expect("Could not generate absolute path to expression file.").to_str().unwrap()) format!("import {}", path.canonicalize().expect("Could not generate absolute path to expression file.").to_str().unwrap())
}; };
let result = hive.introspect(expression).await.unwrap(); let instantiate = local_args.is_present("instantiate");
println!("{}", result); let result = hive.introspect(expression, instantiate).await.unwrap();
if instantiate {
print!("{}", result);
} else {
println!("{}", result);
}
} }

View file

@ -185,10 +185,16 @@ impl Hive {
} }
/// Evaluates an expression using values from the configuration /// Evaluates an expression using values from the configuration
pub async fn introspect(&self, expression: String) -> NixResult<String> { pub async fn introspect(&self, expression: String, instantiate: bool) -> NixResult<String> {
let expression = format!("toJSON (hive.introspect ({}))", expression); if instantiate {
self.nix_instantiate(&expression).eval_with_builders().await? let expression = format!("hive.introspect ({})", expression);
.capture_json().await self.nix_instantiate(&expression).instantiate_with_builders().await?
.capture_output().await
} else {
let expression = format!("toJSON (hive.introspect ({}))", expression);
self.nix_instantiate(&expression).eval_with_builders().await?
.capture_json().await
}
} }
/// Retrieve machinesFile setting for the hive. /// Retrieve machinesFile setting for the hive.