From 3450dc766b813d44c1c13c048059be2fa03542d4 Mon Sep 17 00:00:00 2001 From: Jonas SPRENGER <sprengerjo@gmail.com> Date: Sun, 19 May 2019 16:04:29 +0200 Subject: [PATCH] [feat] conf: add configurable environment variable prefix --- bin/durs-server/src/cli.rs | 4 ++++ lib/core/conf/src/constants.rs | 3 +++ lib/core/conf/src/lib.rs | 27 ++++++++++++++++++--------- lib/core/core/src/commands/mod.rs | 2 ++ lib/core/core/src/lib.rs | 9 ++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/bin/durs-server/src/cli.rs b/bin/durs-server/src/cli.rs index 606d4bc9..55a0ffeb 100644 --- a/bin/durs-server/src/cli.rs +++ b/bin/durs-server/src/cli.rs @@ -57,6 +57,9 @@ pub struct DursCliOpt { /// Set a custom user profile name #[structopt(short = "p", long = "profile-name")] profile_name: Option<String>, + /// Set environment variable prefix + #[structopt(long = "env-prefix")] + env_prefix: Option<String>, } impl ExecutableModuleCommand for DursCliOpt { @@ -85,6 +88,7 @@ impl DursCliOpt { log_stdout: self.log_stdout, profile_name: self.profile_name.clone(), profiles_path: self.profiles_path.clone(), + env_prefix: self.env_prefix.clone(), }; match self.cmd { diff --git a/lib/core/conf/src/constants.rs b/lib/core/conf/src/constants.rs index 1c66f8d4..d1c3cdda 100644 --- a/lib/core/conf/src/constants.rs +++ b/lib/core/conf/src/constants.rs @@ -29,3 +29,6 @@ pub static DEFAULT_CURRRENCY: &'static str = "g1"; /// Default value for `default_sync_module` conf field pub static DEFAULT_DEFAULT_SYNC_MODULE: &'static str = "ws2p1"; + +/// Default environment variable prefix +pub static DEFAULT_ENV_PREFIX: &'static str = "DURS_"; diff --git a/lib/core/conf/src/lib.rs b/lib/core/conf/src/lib.rs index 2d7ee445..9faa8237 100644 --- a/lib/core/conf/src/lib.rs +++ b/lib/core/conf/src/lib.rs @@ -577,9 +577,10 @@ pub fn keypairs_filepath(profiles_path: &Option<PathBuf>, profile: &str) -> Path pub fn load_conf( mut profile_path: PathBuf, keypairs_file_path: &Option<PathBuf>, + env_prefix: &Option<String>, ) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> { // Load conf - let (conf, keypairs) = load_conf_at_path(profile_path.clone(), keypairs_file_path)?; + let (conf, keypairs) = load_conf_at_path(profile_path.clone(), keypairs_file_path, env_prefix)?; // Create currency dir profile_path.push(conf.currency().to_string()); @@ -610,6 +611,7 @@ pub enum DursConfFileError { pub fn load_conf_at_path( profile_path: PathBuf, keypairs_file_path: &Option<PathBuf>, + env_prefix: &Option<String>, ) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> { // Get KeyPairs let keypairs_path = if let Some(ref keypairs_file_path) = keypairs_file_path { @@ -698,12 +700,19 @@ pub fn load_conf_at_path( keypairs }; - let env_conf = envy::from_env::<EnvConf>().unwrap_or_else(|e| { - let err_msg = - format!("Fatal error : fail to parse environment variable : {}", e); - dbg!(&err_msg); - panic!(err_msg); - }); + let env_prefix: &str = if let Some(e) = env_prefix { + &e[..] + } else { + constants::DEFAULT_ENV_PREFIX + }; + + let env_conf = envy::prefixed(env_prefix) + .from_env::<EnvConf>() + .unwrap_or_else(|e| { + let err_msg = format!("Fatal error : fail to parse environment variable : {}", e); + dbg!(&err_msg); + panic!(err_msg); + }); // Open conf file let mut conf_path = profile_path; @@ -865,7 +874,7 @@ mod tests { let profile_path = PathBuf::from("./test/v1/"); save_old_conf(PathBuf::from(profile_path.clone())) .map_err(DursConfFileError::WriteError)?; - let (conf, _keys) = load_conf_at_path(profile_path.clone(), &None)?; + let (conf, _keys) = load_conf_at_path(profile_path.clone(), &None, &None)?; assert_eq!( conf.modules() .get("ws2p") @@ -897,7 +906,7 @@ mod tests { #[test] fn load_conf_file_v2() -> Result<(), DursConfFileError> { let profile_path = PathBuf::from("./test/v2/"); - let (conf, _keys) = load_conf_at_path(profile_path, &None)?; + let (conf, _keys) = load_conf_at_path(profile_path, &None, &None)?; assert_eq!( conf.modules() .get("ws2p") diff --git a/lib/core/core/src/commands/mod.rs b/lib/core/core/src/commands/mod.rs index bea553d7..83cce669 100644 --- a/lib/core/core/src/commands/mod.rs +++ b/lib/core/core/src/commands/mod.rs @@ -45,6 +45,8 @@ pub struct DursCoreOptions { pub profile_name: Option<String>, /// Path where user profiles are persisted pub profiles_path: Option<PathBuf>, + /// Prefix applied to environment variables + pub env_prefix: Option<String>, } /// Durs executable command diff --git a/lib/core/core/src/lib.rs b/lib/core/core/src/lib.rs index 32826c32..8f6d4b3d 100644 --- a/lib/core/core/src/lib.rs +++ b/lib/core/core/src/lib.rs @@ -233,9 +233,12 @@ impl DursCore<DuRsConf> { )?; // Load global conf - let (conf, keypairs) = - durs_conf::load_conf(profile_path.clone(), &durs_core_opts.keypairs_file) - .map_err(DursCoreError::ConfFileError)?; + let (conf, keypairs) = durs_conf::load_conf( + profile_path.clone(), + &durs_core_opts.keypairs_file, + &durs_core_opts.env_prefix, + ) + .map_err(DursCoreError::ConfFileError)?; info!("Success to load global conf."); // Instanciate durs core -- GitLab