Skip to content

Commit

Permalink
Make it possible to use env. variables in config.
Browse files Browse the repository at this point in the history
If there is an env. variable FOO=bar, then we replace $FOO with bar in
the configuration.
  • Loading branch information
brocaar committed Mar 28, 2024
1 parent 7cfcfdb commit f955af7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -18,9 +18,16 @@ pub struct Configuration {
impl Configuration {
pub fn get(filenames: &[String]) -> Result<Configuration> {
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)
}
Expand Down

0 comments on commit f955af7

Please sign in to comment.