From f955af7889808a0b64e215197cae8ae353eaa429 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Thu, 28 Mar 2024 14:53:47 +0000 Subject: [PATCH] Make it possible to use env. variables in config. If there is an env. variable FOO=bar, then we replace $FOO with bar in the configuration. --- src/config.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index cf605bc..44b585d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use std::fs; use std::time::Duration; +use std::{env, fs}; use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -18,9 +18,16 @@ pub struct Configuration { impl Configuration { pub fn get(filenames: &[String]) -> Result { let mut content = String::new(); + for file_name in filenames { content.push_str(&fs::read_to_string(file_name)?); } + + // Replace environment variables in config. + for (k, v) in env::vars() { + content = content.replace(&format!("${}", k), &v); + } + let config: Configuration = toml::from_str(&content)?; Ok(config) }