Skip to content
Snippets Groups Projects
Commit 3450dc76 authored by Jonas SPRENGER's avatar Jonas SPRENGER
Browse files

[feat] conf: add configurable environment variable prefix

parent 2cc69f0b
No related branches found
No related tags found
1 merge request!156Resolve "durs-core: load conf from environment variables as a priority"
...@@ -57,6 +57,9 @@ pub struct DursCliOpt { ...@@ -57,6 +57,9 @@ pub struct DursCliOpt {
/// Set a custom user profile name /// Set a custom user profile name
#[structopt(short = "p", long = "profile-name")] #[structopt(short = "p", long = "profile-name")]
profile_name: Option<String>, profile_name: Option<String>,
/// Set environment variable prefix
#[structopt(long = "env-prefix")]
env_prefix: Option<String>,
} }
impl ExecutableModuleCommand for DursCliOpt { impl ExecutableModuleCommand for DursCliOpt {
...@@ -85,6 +88,7 @@ impl DursCliOpt { ...@@ -85,6 +88,7 @@ impl DursCliOpt {
log_stdout: self.log_stdout, log_stdout: self.log_stdout,
profile_name: self.profile_name.clone(), profile_name: self.profile_name.clone(),
profiles_path: self.profiles_path.clone(), profiles_path: self.profiles_path.clone(),
env_prefix: self.env_prefix.clone(),
}; };
match self.cmd { match self.cmd {
......
...@@ -29,3 +29,6 @@ pub static DEFAULT_CURRRENCY: &'static str = "g1"; ...@@ -29,3 +29,6 @@ pub static DEFAULT_CURRRENCY: &'static str = "g1";
/// Default value for `default_sync_module` conf field /// Default value for `default_sync_module` conf field
pub static DEFAULT_DEFAULT_SYNC_MODULE: &'static str = "ws2p1"; pub static DEFAULT_DEFAULT_SYNC_MODULE: &'static str = "ws2p1";
/// Default environment variable prefix
pub static DEFAULT_ENV_PREFIX: &'static str = "DURS_";
...@@ -577,9 +577,10 @@ pub fn keypairs_filepath(profiles_path: &Option<PathBuf>, profile: &str) -> Path ...@@ -577,9 +577,10 @@ pub fn keypairs_filepath(profiles_path: &Option<PathBuf>, profile: &str) -> Path
pub fn load_conf( pub fn load_conf(
mut profile_path: PathBuf, mut profile_path: PathBuf,
keypairs_file_path: &Option<PathBuf>, keypairs_file_path: &Option<PathBuf>,
env_prefix: &Option<String>,
) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> { ) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> {
// Load conf // 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 // Create currency dir
profile_path.push(conf.currency().to_string()); profile_path.push(conf.currency().to_string());
...@@ -610,6 +611,7 @@ pub enum DursConfFileError { ...@@ -610,6 +611,7 @@ pub enum DursConfFileError {
pub fn load_conf_at_path( pub fn load_conf_at_path(
profile_path: PathBuf, profile_path: PathBuf,
keypairs_file_path: &Option<PathBuf>, keypairs_file_path: &Option<PathBuf>,
env_prefix: &Option<String>,
) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> { ) -> Result<(DuRsConf, DuniterKeyPairs), DursConfFileError> {
// Get KeyPairs // Get KeyPairs
let keypairs_path = if let Some(ref keypairs_file_path) = keypairs_file_path { let keypairs_path = if let Some(ref keypairs_file_path) = keypairs_file_path {
...@@ -698,9 +700,16 @@ pub fn load_conf_at_path( ...@@ -698,9 +700,16 @@ pub fn load_conf_at_path(
keypairs keypairs
}; };
let env_conf = envy::from_env::<EnvConf>().unwrap_or_else(|e| { let env_prefix: &str = if let Some(e) = env_prefix {
let err_msg = &e[..]
format!("Fatal error : fail to parse environment variable : {}", 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); dbg!(&err_msg);
panic!(err_msg); panic!(err_msg);
}); });
...@@ -865,7 +874,7 @@ mod tests { ...@@ -865,7 +874,7 @@ mod tests {
let profile_path = PathBuf::from("./test/v1/"); let profile_path = PathBuf::from("./test/v1/");
save_old_conf(PathBuf::from(profile_path.clone())) save_old_conf(PathBuf::from(profile_path.clone()))
.map_err(DursConfFileError::WriteError)?; .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!( assert_eq!(
conf.modules() conf.modules()
.get("ws2p") .get("ws2p")
...@@ -897,7 +906,7 @@ mod tests { ...@@ -897,7 +906,7 @@ mod tests {
#[test] #[test]
fn load_conf_file_v2() -> Result<(), DursConfFileError> { fn load_conf_file_v2() -> Result<(), DursConfFileError> {
let profile_path = PathBuf::from("./test/v2/"); 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!( assert_eq!(
conf.modules() conf.modules()
.get("ws2p") .get("ws2p")
......
...@@ -45,6 +45,8 @@ pub struct DursCoreOptions { ...@@ -45,6 +45,8 @@ pub struct DursCoreOptions {
pub profile_name: Option<String>, pub profile_name: Option<String>,
/// Path where user profiles are persisted /// Path where user profiles are persisted
pub profiles_path: Option<PathBuf>, pub profiles_path: Option<PathBuf>,
/// Prefix applied to environment variables
pub env_prefix: Option<String>,
} }
/// Durs executable command /// Durs executable command
......
...@@ -233,8 +233,11 @@ impl DursCore<DuRsConf> { ...@@ -233,8 +233,11 @@ impl DursCore<DuRsConf> {
)?; )?;
// Load global conf // Load global conf
let (conf, keypairs) = let (conf, keypairs) = durs_conf::load_conf(
durs_conf::load_conf(profile_path.clone(), &durs_core_opts.keypairs_file) profile_path.clone(),
&durs_core_opts.keypairs_file,
&durs_core_opts.env_prefix,
)
.map_err(DursCoreError::ConfFileError)?; .map_err(DursCoreError::ConfFileError)?;
info!("Success to load global conf."); info!("Success to load global conf.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment