2020-12-18 10:27:44 +01:00
use std ::path ::PathBuf ;
use clap ::{ Arg , App , SubCommand , ArgMatches } ;
2020-12-29 06:35:43 +01:00
use crate ::util ;
2020-12-18 10:27:44 +01:00
pub fn subcommand ( ) -> App < 'static , 'static > {
2020-12-29 20:31:19 +01:00
SubCommand ::with_name ( " introspect " )
2020-12-18 10:27:44 +01:00
. about ( " Evaluate expressions using the complete configuration. " )
. long_about ( r #" Your expression should take an attribute set with keys `pkgs`, `lib` and `nodes` (like a NixOS module) and return a JSON-serializable value.
For example , to retrieve the configuration of one node , you may write something like :
{ nodes , .. . } : nodes . node - a . config . networking . hostName
" #)
. arg ( Arg ::with_name ( " expression_file " )
. index ( 1 )
. help ( " The .nix file containing the expression " )
. takes_value ( true ) )
. arg ( Arg ::with_name ( " expression " )
. short ( " E " )
. help ( " The Nix expression " )
. takes_value ( true ) )
}
pub async fn run ( _global_args : & ArgMatches < '_ > , local_args : & ArgMatches < '_ > ) {
2021-01-24 23:08:48 +01:00
let hive = util ::hive_from_args ( local_args ) . unwrap ( ) ;
2020-12-18 10:27:44 +01:00
if ! ( local_args . is_present ( " expression " ) ^ local_args . is_present ( " expression_file " ) ) {
2020-12-29 20:31:19 +01:00
log ::error! ( " Either an expression (-E) xor 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 ( ) )
} ;
let result = hive . introspect ( expression ) . await . unwrap ( ) ;
println! ( " {} " , result ) ;
}