Crates.io | recursive-env |
lib.rs | recursive-env |
version | 0.1.1 |
source | src |
created_at | 2023-10-10 15:10:48.956991 |
updated_at | 2023-10-10 18:45:02.648345 |
description | Lookup env vars that are defined by other env vars |
homepage | https://github.com/adam-gaia/recursive-env |
repository | https://github.com/adam-gaia/recursive-env |
max_upload_size | |
id | 999183 |
size | 60,166 |
Look up env variables that are built from other env vars
Shell env vars should evaluate recursively
$ TEST1="test1"
$ TEST2="${TEST1} test2"
$ echo $test
test1 test2
But sometimes tooling sets env vars without evaluating them first. Take this simple program:
fn main() {
env::set_var("TEST1", "test1");
env::set_var("TEST2", "${TEST1} test2");
for (key, value) in env::vars() {
println!("{key}: {value}");
}
}
// TODO: rewrite examples here with asserts. Run theses as part of tests
This will set 'TEST2' to the literal string "${TEST1} test2" instead of evaluating to "test1 test2".
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/test`
TEST1: test1
TEST2: ${TEST1} test2
The solution is to get env vars through recursive_env::var()
instead of std::env::var()
use recursive_env::lookup;
fn main() {
env::set_var("TEST1", "test1");
env::set_var("TEST2", "${TEST1} test 2");
for (key, _) in env::vars() {
let fixed = lookup(key)
println!("{key}: {fixed}");
}
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/test`
TEST1: test1
TEST2: test1 test2
use recursive_env::lookup;
fn main() {
let key = "KEY";
let value = String::from("$(subexpression)");
env::set_var(&key, &value);
assert_eq!(lookup::var(key).unwrap(), value);
}