2021-03-02 19:30:43 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Serialized, Toml},
|
2021-03-02 20:43:26 +00:00
|
|
|
util::map,
|
2021-03-02 19:30:43 +00:00
|
|
|
Figment,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-02 19:13:58 +00:00
|
|
|
|
2021-03-02 19:51:33 +00:00
|
|
|
use crate::infra::cli::CLIOpts;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2021-03-02 19:30:43 +00:00
|
|
|
pub struct Configuration {
|
2021-03-02 19:51:33 +00:00
|
|
|
pub log_level_verbose: bool,
|
|
|
|
pub secret_pepper: String,
|
|
|
|
pub some_text: String,
|
2021-03-02 19:30:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:26 +00:00
|
|
|
impl Default for Configuration {
|
|
|
|
fn default() -> Self {
|
2021-03-02 19:30:43 +00:00
|
|
|
Configuration {
|
2021-03-02 20:43:26 +00:00
|
|
|
log_level_verbose: false,
|
2021-03-02 19:30:43 +00:00
|
|
|
secret_pepper: String::from("secretsecretpepper"),
|
|
|
|
some_text: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:26 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:51:33 +00:00
|
|
|
pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
|
2021-03-02 20:43:26 +00:00
|
|
|
let config_file = cli_opts.config_file.clone();
|
|
|
|
|
|
|
|
let config: Configuration = Figment::from(Serialized::defaults(Configuration::default()))
|
|
|
|
.merge(Toml::file(config_file))
|
|
|
|
.merge(Env::prefixed("LLDAP_"))
|
|
|
|
.merge(Configuration::from_cli(cli_opts))
|
|
|
|
.extract()?;
|
2021-03-02 19:30:43 +00:00
|
|
|
|
|
|
|
Ok(config)
|
2021-03-02 19:13:58 +00:00
|
|
|
}
|