Skip to content

Commit

Permalink
Add env_var_with_value() function
Browse files Browse the repository at this point in the history
  • Loading branch information
begoon committed Dec 13, 2024
1 parent 3442c2b commit d19ea3a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
23 changes: 23 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"env" => UnaryOpt(env),
"env_var" => Unary(env_var),
"env_var_or_default" => Binary(env_var_or_default),
"env_var_with_value" => Binary(env_var_with_value),
"error" => Unary(error),
"executable_directory" => Nullary(|_| dir("executable", dirs::executable_dir)),
"extension" => Unary(extension),
Expand Down Expand Up @@ -302,6 +303,28 @@ fn env_var_or_default(context: Context, key: &str, default: &str) -> FunctionRes
}
}

fn env_var_with_value(context: Context, key: &str, default: &str) -> FunctionResult {
use std::env::VarError::*;

if let Some(value) = context.evaluator.context.dotenv.get(key) {
return Ok(value.clone());
}

match env::var(key) {
Err(NotPresent) => Ok(default.to_owned()),
Err(NotUnicode(os_string)) => Err(format!(
"environment variable `{key}` not unicode: {os_string:?}"
)),
Ok(value) => {
if !value.trim().is_empty() {
Ok(value)
} else {
Ok(default.to_owned())
}
}
}
}

fn error(_context: Context, message: &str) -> FunctionResult {
Err(message.to_owned())
}
Expand Down
46 changes: 35 additions & 11 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,26 @@ test! {
name: env_var_functions,
justfile: r"
p := env_var('USER')
b := env_var_or_default('ZADDY', 'HTAP')
x := env_var_or_default('XYZ', 'ABC')
a1 := env_var_or_default('A_UNSET', 'DEFAULT')
a2 := env_var_or_default('A_SET_EMPTY', 'DEFAULT')
a3:= env_var_or_default('A_SET', 'DEFAULT')
z1 := env_var_with_value('Z_UNSET', 'DEFAULT')
z2 := env_var_with_value('Z_SET_EMPTY', 'DEFAULT')
z3 := env_var_with_value('Z_SET', 'DEFAULT')
foo:
/usr/bin/env echo '{{p}}' '{{b}}' '{{x}}'
/usr/bin/env echo '{{p}}' '{{a1}}' '{{a2}}' '{{a3}}' '{{z1}}' '{{z2}}' '{{z3}}'
",
stdout: format!("{} HTAP ABC\n", env::var("USER").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'HTAP' 'ABC'\n", env::var("USER").unwrap()).as_str(),
env: {
"A_SET_EMPTY": "",
"A_SET": "VALUE",
"Z_SET_EMPTY": "",
"Z_SET": "VALUE",
},
stdout: format!("{} DEFAULT VALUE DEFAULT DEFAULT VALUE\n", env::var("USER").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'DEFAULT' '' 'VALUE' 'DEFAULT' 'DEFAULT' 'VALUE'\n", env::var("USER").unwrap()).as_str(),
}

#[cfg(not(windows))]
Expand Down Expand Up @@ -215,15 +227,27 @@ foo:
test! {
name: env_var_functions,
justfile: r#"
p := env_var('USERNAME')
b := env_var_or_default('ZADDY', 'HTAP')
x := env_var_or_default('XYZ', 'ABC')
p := env_var('USER')
a1 := env_var_or_default('A_UNSET', 'DEFAULT')
a2 := env_var_or_default('A_SET_EMPTY', 'DEFAULT')
a3:= env_var_or_default('A_SET', 'DEFAULT')
z1 := env_var_with_value('Z_UNSET', 'DEFAULT')
z2 := env_var_with_value('Z_SET_EMPTY', 'DEFAULT')
z3 := env_var_with_value('Z_SET', 'DEFAULT')
foo:
/usr/bin/env echo '{{p}}' '{{b}}' '{{x}}'
/usr/bin/env echo '{{p}}' '{{a1}}' '{{a2}}' '{{a3}}' '{{z1}}' '{{z2}}' '{{z3}}'
"#,
stdout: format!("{} HTAP ABC\n", env::var("USERNAME").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'HTAP' 'ABC'\n", env::var("USERNAME").unwrap()).as_str(),
env: {
"A_SET_EMPTY": "",
"A_SET": "VALUE",
"Z_SET_EMPTY": "",
"Z_SET": "VALUE",
},
stdout: format!("{} DEFAULT VALUE DEFAULT DEFAULT VALUE\n", env::var("USER").unwrap()).as_str(),
stderr: format!("/usr/bin/env echo '{}' 'DEFAULT' '' 'VALUE' 'DEFAULT' 'DEFAULT' 'VALUE'\n", env::var("USER").unwrap()).as_str(),
}

test! {
Expand Down

0 comments on commit d19ea3a

Please sign in to comment.