Can specify environment variables in client configuration

This commit is contained in:
2022-04-15 21:58:07 +02:00
parent 937343c5f9
commit 489f938b71
6 changed files with 67 additions and 3 deletions

View File

@ -1,3 +1,4 @@
use lazy_regex::regex_find;
use rand::distributions::Alphanumeric;
use rand::Rng;
@ -9,3 +10,24 @@ pub fn rand_str(len: usize) -> String {
.take(len)
.collect()
}
/// Parse environment variables
pub fn apply_env_vars(val: &str) -> String {
let mut val = val.to_string();
if let Some(varname_with_wrapper) = regex_find!(r#"\$\{[a-zA-Z0-9_-]+\}"#, &val) {
let varname = varname_with_wrapper.strip_prefix("${").unwrap().strip_suffix('}').unwrap();
let value = match std::env::var(varname) {
Ok(v) => v,
Err(e) => {
log::error!("Failed to find environment variable {}!", varname);
eprintln!("Failed to find environment variable! {:?}", e);
std::process::exit(2);
}
};
val = val.replace(varname_with_wrapper, &value);
}
val
}