~elchenberg/not-envsubst

ref: f4ab9be4f33746392bb68bbc614cd1283a7dc474 not-envsubst/src/tpl.rs -rw-r--r-- 801 bytes
f4ab9be4elchenberg Initial commit 6 months ago
                                                                                
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
use std::env::var;

use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
    static ref PARAMETERS_REGEX: Regex = Regex::new(r"(?x)
        \$(?P<name>\w[\w\d_]*) # capture the name of parameters without curly braces, eg. $USER
    ").unwrap();
}

pub fn replace_variables(input: Vec<String>) -> Vec<String> {
    let mut output: Vec<String> = Vec::new();
    for line in input {
        let mut clone = line.clone();
        if line.find('$').is_some() {
            for captures in PARAMETERS_REGEX.captures_iter(&line) {
                let name = captures["name"].to_string();
                let value = var(&name).unwrap_or_else(|_| String::from(""));
                clone = clone.replace(&captures[0], &value);
            }
        }
        output.push(clone);
    }
    output
}