Allow loading config files from the CONFIG_FILES env var (#1587)

* Allow loading config files from the CONFIG_FILE env var

* Lint
pull/1594/head
Nam Chu Hoai 2 years ago committed by GitHub
parent 248a3eb968
commit 6f9ce821e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      rust/hyperlane-base/src/macros.rs
  2. 31
      rust/hyperlane-base/src/settings/loader.rs

@ -119,11 +119,6 @@ macro_rules! decl_settings {
#[doc(hidden)]
pub fn _new_settings<'de, T: Deserialize<'de>>(name: &str) -> eyre::Result<T> {
use crate::settings::loader::load_settings_object;
use std::env;
load_settings_object::<T, &str>(
name,
Some(&env::var("BASE_CONFIG").unwrap_or_else(|_| "base".into())),
&[],
)
load_settings_object::<T, &str>(name, &[])
}

@ -27,11 +27,8 @@ use serde::Deserialize;
/// env variable.
pub(crate) fn load_settings_object<'de, T: Deserialize<'de>, S: AsRef<str>>(
agent_prefix: &str,
config_file_name: Option<&str>,
ignore_prefixes: &[S],
) -> Result<T> {
let env = env::var("RUN_ENV").unwrap_or_else(|_| "default".into());
// Derive additional prefix from agent name
let prefix = format!("HYP_{}", agent_prefix).to_ascii_uppercase();
@ -44,20 +41,26 @@ pub(crate) fn load_settings_object<'de, T: Deserialize<'de>, S: AsRef<str>>(
.collect();
let builder = Config::builder();
let builder = if let Some(fname) = config_file_name {
// Load the base config file the old way
let builder = match (env::var("RUN_ENV").ok(), env::var("BASE_CONFIG").ok()) {
(Some(env), Some(fname)) => {
builder.add_source(File::with_name(&format!("./config/{}/{}", env, fname)))
} else {
builder
}
_ => builder,
};
// Load a set of config files
let config_file_paths: Vec<String> = env::var("CONFIG_FILES")
.ok()
.map(|s| s.split(',').map(|s| s.to_string()).collect())
.unwrap_or_default();
let builder = config_file_paths.iter().fold(builder, |builder, path| {
builder.add_source(File::with_name(path))
});
let config_deserializer = builder
.add_source(
File::with_name(&format!(
"./config/{}/{}-partial",
env,
agent_prefix.to_lowercase()
))
.required(false),
)
// Use a base configuration env variable prefix
.add_source(
Environment::with_prefix("HYP_BASE")

Loading…
Cancel
Save