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 21:03:58 +00:00
|
|
|
pub verbose: bool,
|
2021-03-02 19:51:33 +00:00
|
|
|
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 21:03:58 +00:00
|
|
|
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 {
|
2021-03-02 21:03:58 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-03-02 20:43:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|