diff --git a/bin/durs-server/src/cli.rs b/bin/durs-server/src/cli.rs index 606d4bc9fa3aa06c788d34b397cf13cea0f1919e..55a0ffebc5fce48f721c3e5a030c3bd33c92f2ca 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 1c66f8d44b0af51efe5b2ef282d2fec436cfa8f3..d1c3cdda62bf18ad1932cd3f17be106a0d655588 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 2d7ee44580afcd177972e0593b6cb1891fae6276..9faa8237476afef8d3af2192cf769f90fab4f7c7 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 bea553d7be56134be34c2333da12d554d4708eea..83cce669ea4b852775a71dfce15535125d3686c2 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 32826c329cbb1e2dfc7cb7dfa011d0a2c7864d8a..8f6d4b3d48cbc062eab03ecdb4da70486739d770 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