src/global/functions/envs.rs

27 lines · 860 bytes

3 symbols in this file
 1use std::env;
 2
 3/// removes all environment variables in a vector
 4pub fn clear_envs(modified: &mut Vec<(String, String)>) {
 5    modified.iter().for_each(|(key, _)| env::remove_var(key));
 6    modified.clear();
 7}
 8
 9/// set all environment variables in an interator, while pushing changes into the modified vector
10/// to keep track of changes
11pub fn set_envs<T: Iterator<Item = (String, String)>>(
12    envs: T,
13    modified: &mut Vec<(String, String)>,
14) {
15    envs.for_each(|(key, value)| {
16        if !modified.iter().any(|(k, _)| k == &key) {
17            modified.push((key.clone(), value.clone()));
18        }
19        env::set_var(key, value);
20    });
21}
22
23/// replace env placeholder in a string with the value of the env
24pub fn apply_envs(mut s: String) -> String {
25    env::vars().for_each(|(key, value)| s = s.replace(&format!("${{{key}}}"), &value));
26    s
27}