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
Branches
Tags
1 merge request!156Resolve "durs-core: load conf from environment variables as a priority"
......@@ -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 {
......
......@@ -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_";
......@@ -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,9 +700,16 @@ 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);
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);
});
......@@ -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")
......
......@@ -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
......
......@@ -233,8 +233,11 @@ impl DursCore<DuRsConf> {
)?;
// Load global conf
let (conf, keypairs) =
durs_conf::load_conf(profile_path.clone(), &durs_core_opts.keypairs_file)
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.");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment