Skip to content

Commit d382d74

Browse files
authored
feat: Add environment variable substitution in config files (#1577)
closes #942 This PR brings support for environment variable substitution in config files. This simply uses the [subst](https://github.com/fizyr/subst) crate to parse all environment variables before parsing the toml, this allows to define toml values in environment variables for more flexibility.
1 parent 7d7304f commit d382d74

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ opentelemetry = { version = "0.30.0", default-features = false, features = ["met
139139
opentelemetry-otlp = { version = "0.30.0", features = ["metrics"], optional = true }
140140
opentelemetry_sdk = { version = "0.30.0", default-features = false, features = ["metrics"], optional = true }
141141
rhai = { version = "1", features = ["sync", "serde", "no_optimize", "no_module", "no_custom_syntax", "only_i64"], optional = true }
142+
subst = "0.3.8"
142143

143144
[dev-dependencies]
144145
abscissa_core = { version = "0.8.2", default-features = false, features = ["testing"] }

src/config.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
path::PathBuf,
1414
};
1515

16-
use abscissa_core::{FrameworkError, config::Config, path::AbsPathBuf};
16+
use abscissa_core::{FrameworkError, FrameworkErrorKind, config::Config, path::AbsPathBuf};
1717
use anyhow::{Result, anyhow};
1818
use clap::{Parser, ValueHint};
1919
use conflate::Merge;
@@ -120,7 +120,17 @@ impl RusticConfig {
120120

121121
if let Some(path) = paths.iter().find(|path| path.exists()) {
122122
merge_logs.push((Level::Info, format!("using config {}", path.display())));
123-
let mut config = Self::load_toml_file(AbsPathBuf::canonicalize(path)?)?;
123+
let config_content = subst::substitute(
124+
&std::fs::read_to_string(AbsPathBuf::canonicalize(path)?)?,
125+
&subst::Env,
126+
)
127+
.map_err(|e| {
128+
abscissa_core::error::context::Context::new(
129+
FrameworkErrorKind::ParseError,
130+
Some(Box::new(e)),
131+
)
132+
})?;
133+
let mut config = Self::load_toml(config_content)?;
124134
// if "use_profile" is defined in config file, merge the referenced profiles first
125135
for profile in &config.global.use_profiles.clone() {
126136
config.merge_profile(profile, merge_logs, Level::Warn)?;

0 commit comments

Comments
 (0)