From 1d21ba0ecc908a9189d45c2ebf44d735b013021e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 30 Dec 2021 08:50:06 +0100 Subject: [PATCH] Parse environment variables in all banner configuration fields --- src/data/config.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/data/config.rs b/src/data/config.rs index 379dd23..42b20ff 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -89,6 +89,14 @@ pub struct Config { static mut CONF: Option = None; impl Config { + fn parse_string_val(val: String) -> String { + if val.starts_with("$") { + std::env::var(&val[1..]).unwrap() + } else { + val + } + } + fn yaml_str(parsed: &Yaml, name: &str) -> String { let val = match &parsed[name] { Yaml::Real(r) | Yaml::String(r) => r.to_string(), @@ -104,11 +112,7 @@ impl Config { }.to_string(); // Check if we have to get an environment variable - if val.starts_with("$") { - std::env::var(&val[1..]).unwrap() - } else { - val - } + Self::parse_string_val(val) } fn yaml_u64(parsed: &Yaml, name: &str) -> u64 { @@ -184,9 +188,14 @@ impl Config { v => panic!("Invalid banner nature: {} !", v) }, message: parsed_banner["message"].as_hash().unwrap().iter() - .map(|(k, v)| (k.as_str().unwrap().to_string(), v.as_str().unwrap().to_string())) + .map(|(k, v)| ( + k.as_str().unwrap().to_string(), + Self::parse_string_val(v.as_str().unwrap().to_string())) + ) .collect(), - link: parsed_banner["link"].as_str().map(|s| s.to_string()), + link: parsed_banner["link"].as_str() + .map(str::to_string) + .map(Self::parse_string_val), }) };