lldap/src/infra/configuration.rs

60 lines
1.4 KiB
Rust
Raw Normal View History

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 22:07:01 +00:00
pub ldap_port: u16,
pub ldaps_port: u16,
2021-03-02 19:51:33 +00:00
pub secret_pepper: String,
pub some_text: String,
2021-03-02 22:07:01 +00:00
pub verbose: bool,
2021-03-02 19:30:43 +00:00
}
impl Default for Configuration {
fn default() -> Self {
2021-03-02 19:30:43 +00:00
Configuration {
2021-03-02 22:07:01 +00:00
ldap_port: 3890,
ldaps_port: 6360,
2021-03-02 19:30:43 +00:00
secret_pepper: String::from("secretsecretpepper"),
some_text: String::new(),
2021-03-02 22:07:01 +00:00
verbose: false,
2021-03-02 19:30:43 +00:00
}
}
}
impl Configuration {
2021-03-02 22:07:01 +00:00
fn merge_with_cli(mut self: Configuration, cli_opts: CLIOpts) -> Configuration {
if cli_opts.verbose {
2021-03-02 22:07:01 +00:00
self.verbose = true;
}
if let Some(port) = cli_opts.ldap_port {
self.ldap_port = port;
}
if let Some(port) = cli_opts.ldaps_port {
self.ldaps_port = port;
}
2021-03-02 22:07:01 +00:00
self
}
}
2021-03-02 19:51:33 +00:00
pub fn init(cli_opts: CLIOpts) -> Result<Configuration> {
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_"))
.extract()?;
2021-03-02 19:30:43 +00:00
2021-03-02 22:07:01 +00:00
let config = config.merge_with_cli(cli_opts);
2021-03-02 19:30:43 +00:00
Ok(config)
2021-03-02 19:13:58 +00:00
}