Skip to content

Commit 2e82b49

Browse files
committed
implement process variable lookup via the functions processEnv or process_env or the virtuell object env
## examples - `{{ processEnv("USER") }}` - `{{ process_env("USER") }}` - `{{ env.USER }}` Signed-off-by: Sven Kanoldt <[email protected]>
1 parent d2cfbfd commit 2e82b49

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

Cargo.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

curlz/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ dialoguer = "0.10"
2121
filenamify = "0.1"
2222
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2323

24-
minijinja = "0.29"
24+
minijinja = "0.30"
2525

2626
jsonwebtoken = "8.1"
2727
serde_json = { version = "1.0", features = ["preserve_order"] }
2828

2929
## experimental
3030
pest = { version = "2.4", optional = true }
3131
pest_derive = { version = "2.4", optional = true }
32+
minijinja-stack-ref = "0.30"
3233

3334
[features]
3435
"x-http-lang" = ['dep:pest', 'dep:pest_derive']

curlz/src/curlz/template/functions/process_env.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
use minijinja::value::Value;
2-
use minijinja::{Error, ErrorKind, State};
3-
use std::env::VarError;
1+
use minijinja::value::{StructObject, Value};
2+
use minijinja::{Error, State};
43

5-
pub fn process_env(_state: &State, var_name: &Value) -> Result<String, Error> {
6-
let var_name = var_name.as_str().ok_or_else(|| {
7-
Error::new(
8-
ErrorKind::MissingArgument,
9-
"The argument `var_name` for function `processEnv(var_name)` is missing.",
10-
)
11-
})?;
4+
pub struct ProcessEnv;
5+
impl StructObject for ProcessEnv {
6+
fn get_field(&self, field: &str) -> Option<Value> {
7+
// std::env::var(var_name)
8+
// .map(|v| Value::from_safe_string(v))
9+
// .map_err(|e| match e {
10+
// VarError::NotPresent => Error::new(
11+
// ErrorKind::NonKey,
12+
// format!("The process env variable `{var_name}` is not defined."),
13+
// ),
14+
// VarError::NotUnicode(_) => Error::new(
15+
// ErrorKind::UndefinedError,
16+
// format!("The process env variable `{var_name}` has an invalid unicode value."),
17+
// ),
18+
// })
19+
std::env::var(field).map(Value::from_safe_string).ok()
20+
}
21+
}
1222

13-
std::env::var(var_name).map_err(|e| match e {
14-
VarError::NotPresent => Error::new(
15-
ErrorKind::NonKey,
16-
format!("The process env variable `{var_name}` is not defined."),
17-
),
18-
VarError::NotUnicode(_) => Error::new(
19-
ErrorKind::UndefinedError,
20-
format!("The process env variable `{var_name}` has an invalid unicode value."),
21-
),
22-
})
23+
/// function for minijinja
24+
pub fn process_env(_: &State, var_name: &str) -> Result<Value, Error> {
25+
Ok(ProcessEnv.get_field(var_name).unwrap_or_default())
2326
}
2427

2528
#[cfg(test)]
2629
mod tests {
30+
use crate::domain::environment::Environment;
31+
use crate::template::Renderer;
2732
use crate::test_utils::RenderBuilder;
2833
use std::collections::HashMap;
2934

@@ -51,6 +56,21 @@ mod tests {
5156
);
5257
}
5358

59+
#[test]
60+
fn should_resolve_lazy_via_env_virtuell_object() {
61+
let mut r = Renderer::new(&Environment::default());
62+
#[cfg(not(windows))]
63+
assert_eq!(
64+
r.render(r#"{{ env.USER }}"#, "template").unwrap(),
65+
std::env::var("USER").unwrap()
66+
);
67+
#[cfg(windows)]
68+
assert_eq!(
69+
r.render(r#"{{ env.USERNAME }}"#, "template").unwrap(),
70+
std::env::var("USERNAME").unwrap()
71+
);
72+
}
73+
5474
#[test]
5575
fn should_provide_process_env_var_via_env_object() {
5676
// TODO: this is just a toy around case

curlz/src/curlz/template/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use functions::process_env::process_env;
66
use functions::prompt::{prompt_for, prompt_password};
77

88
use crate::domain::environment::Environment;
9+
use crate::template::functions::process_env::ProcessEnv;
10+
911
use minijinja::value::Value;
1012
use minijinja::Environment as MEnvironment;
1113

@@ -31,6 +33,9 @@ impl<'source> Renderer<'source> {
3133
env.add_function("prompt_for", prompt_for);
3234
env.add_function("jwt", jwt);
3335

36+
// this provides lazy env var lookup
37+
env.add_global("env", Value::from_struct_object(ProcessEnv));
38+
3439
Self { env, ctx }
3540
}
3641

0 commit comments

Comments
 (0)