2021-03-02 19:30:43 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Serialized, Toml},
|
|
|
|
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 19:51:33 +00:00
|
|
|
impl Configuration {
|
|
|
|
fn from_cli(cli_opts: CLIOpts) -> Self {
|
2021-03-02 19:30:43 +00:00
|
|
|
Configuration {
|
2021-03-02 19:51:33 +00:00
|
|
|
log_level_verbose: cli_opts.verbose,
|
2021-03-02 19:30:43 +00:00
|
|
|
secret_pepper: String::from("secretsecretpepper"),
|
|
|
|
some_text: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:51:33 +00:00
|
|
|
pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
|
|
|
|
// FIXME cli arguments are less prioritary than toml config file or env... Not good.
|
|
|
|
let config: Configuration = Figment::from(Serialized::defaults(Configuration::from_cli(
|
|
|
|
cli_opts.clone(),
|
|
|
|
)))
|
|
|
|
.merge(Toml::file(cli_opts.config_file))
|
|
|
|
.merge(Env::prefixed("LLDAP_"))
|
|
|
|
.extract()?;
|
2021-03-02 19:30:43 +00:00
|
|
|
|
|
|
|
Ok(config)
|
2021-03-02 19:13:58 +00:00
|
|
|
}
|