Add support for --show-trace

This commit is contained in:
Zhaofeng Li 2020-12-28 21:35:43 -08:00
parent ab6515d935
commit ed52e259aa
6 changed files with 45 additions and 19 deletions

View file

@ -33,11 +33,11 @@ pub fn subcommand() -> App<'static, 'static> {
.long_help("Deactivates the progress spinner and prints every line of output.") .long_help("Deactivates the progress spinner and prints every line of output.")
.takes_value(false)); .takes_value(false));
util::register_common_args(command) util::register_selector_args(command)
} }
pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) { pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
let mut hive = Hive::from_config_arg(local_args).unwrap(); let mut hive = Hive::from_args(local_args).unwrap();
println!("Enumerating nodes..."); println!("Enumerating nodes...");
let all_nodes = hive.deployment_info().await.unwrap(); let all_nodes = hive.deployment_info().await.unwrap();

View file

@ -6,9 +6,10 @@ use tokio::process::Command;
use crate::nix::{Hive, DeploymentTask, DeploymentGoal, Host}; use crate::nix::{Hive, DeploymentTask, DeploymentGoal, Host};
use crate::nix::host; use crate::nix::host;
use crate::util;
pub fn subcommand() -> App<'static, 'static> { pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("apply-local") let command = SubCommand::with_name("apply-local")
.about("Apply configurations on the local machine") .about("Apply configurations on the local machine")
.arg(Arg::with_name("goal") .arg(Arg::with_name("goal")
.help("Deployment goal") .help("Deployment goal")
@ -29,7 +30,9 @@ pub fn subcommand() -> App<'static, 'static> {
.arg(Arg::with_name("we-are-launched-by-sudo") .arg(Arg::with_name("we-are-launched-by-sudo")
.long("we-are-launched-by-sudo") .long("we-are-launched-by-sudo")
.hidden(true) .hidden(true)
.takes_value(false)) .takes_value(false));
util::register_common_args(command)
} }
pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) { pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
@ -62,7 +65,7 @@ pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
} }
} }
let mut hive = Hive::from_config_arg(local_args).unwrap(); let mut hive = Hive::from_args(local_args).unwrap();
let hostname = hostname::get().expect("Could not get hostname") let hostname = hostname::get().expect("Could not get hostname")
.to_string_lossy().into_owned(); .to_string_lossy().into_owned();
let goal = DeploymentGoal::from_str(local_args.value_of("goal").unwrap()).unwrap(); let goal = DeploymentGoal::from_str(local_args.value_of("goal").unwrap()).unwrap();

View file

@ -13,11 +13,11 @@ pub fn subcommand() -> App<'static, 'static> {
.long_help("Deactivates the progress spinner and prints every line of output.") .long_help("Deactivates the progress spinner and prints every line of output.")
.takes_value(false)); .takes_value(false));
util::register_common_args(command) util::register_selector_args(command)
} }
pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) { pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
let mut hive = Hive::from_config_arg(local_args).unwrap(); let mut hive = Hive::from_args(local_args).unwrap();
println!("Enumerating nodes..."); println!("Enumerating nodes...");
let all_nodes = hive.deployment_info().await.unwrap(); let all_nodes = hive.deployment_info().await.unwrap();

View file

@ -3,6 +3,7 @@ use std::path::PathBuf;
use clap::{Arg, App, SubCommand, ArgMatches}; use clap::{Arg, App, SubCommand, ArgMatches};
use crate::nix::Hive; use crate::nix::Hive;
use crate::util;
pub fn subcommand() -> App<'static, 'static> { pub fn subcommand() -> App<'static, 'static> {
let command = SubCommand::with_name("introspect") let command = SubCommand::with_name("introspect")
@ -29,11 +30,11 @@ For example, to retrieve the configuration of one node, you may write something
.required(true)) .required(true))
; ;
command util::register_common_args(command)
} }
pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) { pub async fn run(_global_args: &ArgMatches<'_>, local_args: &ArgMatches<'_>) {
let mut hive = Hive::from_config_arg(local_args).unwrap(); let mut hive = Hive::from_args(local_args).unwrap();
if !(local_args.is_present("expression") ^ local_args.is_present("expression_file")) { if !(local_args.is_present("expression") ^ local_args.is_present("expression_file")) {
eprintln!("Either an expression (-E) xor a .nix file containing an expression should be specified, not both."); eprintln!("Either an expression (-E) xor a .nix file containing an expression should be specified, not both.");

View file

@ -56,6 +56,7 @@ pub struct Hive {
hive: PathBuf, hive: PathBuf,
eval_nix: TempPath, eval_nix: TempPath,
builder: Box<dyn Host>, builder: Box<dyn Host>,
show_trace: bool,
} }
impl Hive { impl Hive {
@ -67,14 +68,21 @@ impl Hive {
hive: hive.as_ref().to_owned(), hive: hive.as_ref().to_owned(),
eval_nix: eval_nix.into_temp_path(), eval_nix: eval_nix.into_temp_path(),
builder: host::local(), builder: host::local(),
show_trace: false,
}) })
} }
pub fn from_config_arg(args: &ArgMatches<'_>) -> NixResult<Self> { pub fn from_args(args: &ArgMatches<'_>) -> NixResult<Self> {
let path = args.value_of("config").expect("The config arg should exist").to_owned(); let path = args.value_of("config").expect("The config arg should exist").to_owned();
let path = canonicalize_path(path); let path = canonicalize_path(path);
Self::new(path) let mut hive = Self::new(path)?;
if args.is_present("show-trace") {
hive.show_trace = true;
}
Ok(hive)
} }
/// Retrieve deployment info for all nodes /// Retrieve deployment info for all nodes
@ -126,7 +134,7 @@ impl Hive {
} }
fn nix_instantiate(&self, expression: &str) -> NixInstantiate { fn nix_instantiate(&self, expression: &str) -> NixInstantiate {
NixInstantiate::new(&self.eval_nix, &self.hive, expression.to_owned()) NixInstantiate::new(&self, expression.to_owned())
} }
} }
@ -218,17 +226,15 @@ impl DeploymentGoal {
} }
struct NixInstantiate<'hive> { struct NixInstantiate<'hive> {
eval_nix: &'hive Path, hive: &'hive Hive,
hive: &'hive Path,
expression: String, expression: String,
} }
impl<'hive> NixInstantiate<'hive> { impl<'hive> NixInstantiate<'hive> {
fn new(eval_nix: &'hive Path, hive: &'hive Path, expression: String) -> Self { fn new(hive: &'hive Hive, expression: String) -> Self {
Self { Self {
eval_nix,
expression,
hive, hive,
expression,
} }
} }
@ -242,10 +248,15 @@ impl<'hive> NixInstantiate<'hive> {
.arg("-E") .arg("-E")
.arg(format!( .arg(format!(
"with builtins; let eval = import {}; hive = eval {{ rawHive = import {}; }}; in {}", "with builtins; let eval = import {}; hive = eval {{ rawHive = import {}; }}; in {}",
self.eval_nix.to_str().unwrap(), self.hive.eval_nix.to_str().unwrap(),
self.hive.to_str().unwrap(), self.hive.hive.to_str().unwrap(),
self.expression, self.expression,
)); ));
if self.hive.show_trace {
command.arg("--show-trace");
}
command command
} }

View file

@ -56,6 +56,17 @@ pub fn register_common_args<'a, 'b>(command: App<'a, 'b>) -> App<'a, 'b> {
.help("Path to a Hive expression") .help("Path to a Hive expression")
.default_value("hive.nix") .default_value("hive.nix")
.required(true)) .required(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")
.takes_value(false))
}
pub fn register_selector_args<'a, 'b>(command: App<'a, 'b>) -> App<'a, 'b> {
let command = register_common_args(command);
command
.arg(Arg::with_name("on") .arg(Arg::with_name("on")
.long("on") .long("on")
.help("Select a list of machines") .help("Select a list of machines")