1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 05:19:21 +00:00

Parse environment variables in all banner configuration fields

This commit is contained in:
Pierre HUBERT 2021-12-30 08:50:06 +01:00
parent bbe8252f9d
commit 1d21ba0ecc

View File

@ -89,6 +89,14 @@ pub struct Config {
static mut CONF: Option<Config> = 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),
})
};