From ffce735b79f3b71bf0d6239c42938ac5d3182002 Mon Sep 17 00:00:00 2001 From: Thomas Wickham Date: Tue, 2 Mar 2021 22:03:58 +0100 Subject: [PATCH] Fix bug in config from cli flags The flag value (true/false) was always provided to the configuration, which means that the cli was overriding everything else. --- src/infra/configuration.rs | 14 +++++++++----- src/infra/logging.rs | 2 +- src/main.rs | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/infra/configuration.rs b/src/infra/configuration.rs index 1987383..58c62a3 100644 --- a/src/infra/configuration.rs +++ b/src/infra/configuration.rs @@ -10,7 +10,7 @@ use crate::infra::cli::CLIOpts; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Configuration { - pub log_level_verbose: bool, + pub verbose: bool, pub secret_pepper: String, pub some_text: String, } @@ -18,7 +18,7 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Configuration { - log_level_verbose: false, + verbose: false, secret_pepper: String::from("secretsecretpepper"), some_text: String::new(), } @@ -27,9 +27,13 @@ impl Default for Configuration { impl Configuration { fn from_cli(cli_opts: CLIOpts) -> Figment { - let config_opts_from_cli = map! { - "log_level_verbose" => cli_opts.verbose - }; + let mut config_opts_from_cli = map!(); + + // XXX only add the option if given so that the `false` value don't override a + // previous configuration + if cli_opts.verbose { + config_opts_from_cli.insert("verbose", true); + } Figment::new().join(Serialized::defaults(config_opts_from_cli)) } diff --git a/src/infra/logging.rs b/src/infra/logging.rs index 40e14f3..939ee9a 100644 --- a/src/infra/logging.rs +++ b/src/infra/logging.rs @@ -17,7 +17,7 @@ pub fn init(config: Configuration) -> anyhow::Result<()> { } fn log_level_from_config(config: Configuration) -> tracing::Level { - if config.log_level_verbose { + if config.verbose { tracing::Level::DEBUG } else { tracing::Level::INFO diff --git a/src/main.rs b/src/main.rs index a42b692..fc7c712 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,10 @@ fn main() -> Result<()> { let config = infra::configuration::init(cli_opts.clone())?; infra::logging::init(config.clone())?; - info!("Starting...."); - debug!("Config: {:?}", config); - debug!("CLI: {:?}", cli_opts); + info!("Starting LLDAP...."); + + debug!("CLI: {:#?}", cli_opts); + debug!("Configuration: {:#?}", config); info!("End."); Ok(()) }