Fix cli arg precedence and finish config wiring

This commit is contained in:
Thomas Wickham 2021-03-02 21:43:26 +01:00
parent 0b6f482e51
commit 009ffd793b
3 changed files with 33 additions and 14 deletions

View File

@ -1,2 +1,2 @@
# LLDAP configuration # LLDAP configuration
some_text = "ok boomer" some_text = "ok boomer from toml file"

View File

@ -1,6 +1,7 @@
use anyhow::Result; use anyhow::Result;
use figment::{ use figment::{
providers::{Env, Format, Serialized, Toml}, providers::{Env, Format, Serialized, Toml},
util::map,
Figment, Figment,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -14,23 +15,33 @@ pub struct Configuration {
pub some_text: String, pub some_text: String,
} }
impl Configuration { impl Default for Configuration {
fn from_cli(cli_opts: CLIOpts) -> Self { fn default() -> Self {
Configuration { Configuration {
log_level_verbose: cli_opts.verbose, log_level_verbose: false,
secret_pepper: String::from("secretsecretpepper"), secret_pepper: String::from("secretsecretpepper"),
some_text: String::new(), some_text: String::new(),
} }
} }
} }
impl Configuration {
fn from_cli(cli_opts: CLIOpts) -> Figment {
let config_opts_from_cli = map! {
"log_level_verbose" => cli_opts.verbose
};
Figment::new().join(Serialized::defaults(config_opts_from_cli))
}
}
pub fn init(cli_opts: CLIOpts) -> Result<Configuration> { pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
// FIXME cli arguments are less prioritary than toml config file or env... Not good. let config_file = cli_opts.config_file.clone();
let config: Configuration = Figment::from(Serialized::defaults(Configuration::from_cli(
cli_opts.clone(), let config: Configuration = Figment::from(Serialized::defaults(Configuration::default()))
))) .merge(Toml::file(config_file))
.merge(Toml::file(cli_opts.config_file))
.merge(Env::prefixed("LLDAP_")) .merge(Env::prefixed("LLDAP_"))
.merge(Configuration::from_cli(cli_opts))
.extract()?; .extract()?;
Ok(config) Ok(config)

View File

@ -3,15 +3,23 @@ use anyhow::Context;
use tracing::subscriber::set_global_default; use tracing::subscriber::set_global_default;
use tracing_log::LogTracer; use tracing_log::LogTracer;
pub fn init(_config: Configuration) -> anyhow::Result<()> { pub fn init(config: Configuration) -> anyhow::Result<()> {
// TODO: use config.log_level_verbose to set level let max_log_level = log_level_from_config(config);
let subscriber = tracing_subscriber::fmt() let subscriber = tracing_subscriber::fmt()
.with_timer(tracing_subscriber::fmt::time::time()) .with_timer(tracing_subscriber::fmt::time::time())
.with_target(false) .with_target(false)
.with_level(true) .with_level(true)
.with_max_level(max_log_level)
.finish(); .finish();
LogTracer::init().context("Failed to set logger")?; LogTracer::init().context("Failed to set logger")?;
set_global_default(subscriber).context("Failed to set subscriber")?; set_global_default(subscriber).context("Failed to set subscriber")?;
Ok(()) Ok(())
} }
fn log_level_from_config(config: Configuration) -> tracing::Level {
if config.log_level_verbose {
tracing::Level::DEBUG
} else {
tracing::Level::INFO
}
}