-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.rs
More file actions
51 lines (42 loc) · 1.39 KB
/
Copy pathvariables.rs
File metadata and controls
51 lines (42 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::collections::HashMap;
use std::sync::LazyLock;
use std::sync::Mutex;
pub(crate) static VARS: LazyLock<Mutex<HashMap<String, String>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
pub(crate) fn expand_vars(args: &mut [String]) {
let vars = VARS.lock().unwrap();
for arg in args.iter_mut() {
if !arg.contains("$") {
continue;
}
let mut result = String::new();
let mut chars = arg.chars().peekable();
while let Some(c) = chars.next() {
match c {
'$' => {
let mut var = String::new();
let with_curly = chars.peek() == Some(&'{');
if with_curly {
chars.next();
}
for c in chars.by_ref() {
if with_curly && c == '}' {
break;
} else {
var.push(c);
}
}
if let Some(value) = vars.get(&var) {
result.push_str(value);
} else if let Ok(value) = std::env::var(&var) {
result.push_str(&value);
}
}
_ => {
result.push(c);
}
}
}
*arg = result;
}
}