From ed52e259aa2cdb8e93968e110b48336cdfa8c4ec Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Mon, 28 Dec 2020 21:35:43 -0800 Subject: [PATCH] Add support for --show-trace --- src/command/apply.rs | 4 ++-- src/command/apply_local.rs | 9 ++++++--- src/command/build.rs | 4 ++-- src/command/introspect.rs | 5 +++-- src/nix/mod.rs | 31 +++++++++++++++++++++---------- src/util.rs | 11 +++++++++++ 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/command/apply.rs b/src/command/apply.rs index 0529bc1..58bdc82 100644 --- a/src/command/apply.rs +++ b/src/command/apply.rs @@ -33,11 +33,11 @@ pub fn subcommand() -> App<'static, 'static> { .long_help("Deactivates the progress spinner and prints every line of output.") .takes_value(false)); - util::register_common_args(command) + util::register_selector_args(command) } 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..."); let all_nodes = hive.deployment_info().await.unwrap(); diff --git a/src/command/apply_local.rs b/src/command/apply_local.rs index 7dc1689..9d18062 100644 --- a/src/command/apply_local.rs +++ b/src/command/apply_local.rs @@ -6,9 +6,10 @@ use tokio::process::Command; use crate::nix::{Hive, DeploymentTask, DeploymentGoal, Host}; use crate::nix::host; +use crate::util; 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") .arg(Arg::with_name("goal") .help("Deployment goal") @@ -29,7 +30,9 @@ pub fn subcommand() -> App<'static, 'static> { .arg(Arg::with_name("we-are-launched-by-sudo") .long("we-are-launched-by-sudo") .hidden(true) - .takes_value(false)) + .takes_value(false)); + + util::register_common_args(command) } 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") .to_string_lossy().into_owned(); let goal = DeploymentGoal::from_str(local_args.value_of("goal").unwrap()).unwrap(); diff --git a/src/command/build.rs b/src/command/build.rs index 215a637..a2fca67 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -13,11 +13,11 @@ pub fn subcommand() -> App<'static, 'static> { .long_help("Deactivates the progress spinner and prints every line of output.") .takes_value(false)); - util::register_common_args(command) + util::register_selector_args(command) } 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..."); let all_nodes = hive.deployment_info().await.unwrap(); diff --git a/src/command/introspect.rs b/src/command/introspect.rs index d38bbb7..3614ee8 100644 --- a/src/command/introspect.rs +++ b/src/command/introspect.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use clap::{Arg, App, SubCommand, ArgMatches}; use crate::nix::Hive; +use crate::util; pub fn subcommand() -> App<'static, 'static> { 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)) ; - command + util::register_common_args(command) } 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")) { eprintln!("Either an expression (-E) xor a .nix file containing an expression should be specified, not both."); diff --git a/src/nix/mod.rs b/src/nix/mod.rs index 1c9add0..ad9e7ee 100644 --- a/src/nix/mod.rs +++ b/src/nix/mod.rs @@ -56,6 +56,7 @@ pub struct Hive { hive: PathBuf, eval_nix: TempPath, builder: Box, + show_trace: bool, } impl Hive { @@ -67,14 +68,21 @@ impl Hive { hive: hive.as_ref().to_owned(), eval_nix: eval_nix.into_temp_path(), builder: host::local(), + show_trace: false, }) } - pub fn from_config_arg(args: &ArgMatches<'_>) -> NixResult { + pub fn from_args(args: &ArgMatches<'_>) -> NixResult { let path = args.value_of("config").expect("The config arg should exist").to_owned(); 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 @@ -126,7 +134,7 @@ impl Hive { } 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> { - eval_nix: &'hive Path, - hive: &'hive Path, + hive: &'hive Hive, expression: String, } 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 { - eval_nix, - expression, hive, + expression, } } @@ -242,10 +248,15 @@ impl<'hive> NixInstantiate<'hive> { .arg("-E") .arg(format!( "with builtins; let eval = import {}; hive = eval {{ rawHive = import {}; }}; in {}", - self.eval_nix.to_str().unwrap(), - self.hive.to_str().unwrap(), + self.hive.eval_nix.to_str().unwrap(), + self.hive.hive.to_str().unwrap(), self.expression, )); + + if self.hive.show_trace { + command.arg("--show-trace"); + } + command } diff --git a/src/util.rs b/src/util.rs index 4db0b1f..ba09b89 100644 --- a/src/util.rs +++ b/src/util.rs @@ -56,6 +56,17 @@ pub fn register_common_args<'a, 'b>(command: App<'a, 'b>) -> App<'a, 'b> { .help("Path to a Hive expression") .default_value("hive.nix") .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") .long("on") .help("Select a list of machines")