Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/content/intro/feature-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,6 @@ Example uses include referencing environmental variables in the `floki` config,
docker_switches:
- -v {{ env.HOME }}/.vim:/home/build/.vim
```
`get_env(name="HOME")` is also available, and takes an optional `default` for when the variable isn't set.

Note that extensive use may reduce the reproducibility and shareability of your `floki.yaml`.
43 changes: 43 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ fn makeloader(
}
}

// Reimplementation of tera 1's `get_env`, which was dropped in tera 2.
fn get_env(kwargs: tera::Kwargs, _: &tera::State) -> tera::TeraResult<tera::Value> {
let name: String = kwargs.must_get("name")?;
match std::env::var(&name) {
Ok(value) => Ok(value.into()),
Err(_) => match kwargs.get::<tera::Value>("default")? {
Some(default) => Ok(default),
None => Err(tera::Error::message(format!(
"Environment variable `{name}` not found"
))),
},
}
}

// Renders a template from a given string.
pub fn render_template(template: &str, source_filename: &Path) -> Result<String, FlokiError> {
let template_path = source_filename.display().to_string();
Expand All @@ -216,6 +230,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
tera.register_function("yaml", makeloader(&canonical_path, LoaderType::Yaml));
tera.register_function("json", makeloader(&canonical_path, LoaderType::Json));
tera.register_function("toml", makeloader(&canonical_path, LoaderType::Toml));
tera.register_function("get_env", get_env);

tera.add_raw_template(&template_path, template)
.map_err(|e| FlokiError::ProblemRenderingTemplate {
Expand Down Expand Up @@ -413,6 +428,34 @@ mod test {
Ok(())
}

#[test]
fn test_tera_get_env() -> Result<(), Box<dyn std::error::Error>> {
// Uses PATH rather than setting a var, as set_var is unsound in a threaded
// test binary.
let template = r#"shell: {{ get_env(name="PATH") }}"#;
assert_eq!(
render_template(template, Path::new("floki.yaml"))?,
format!("shell: {}", std::env::var("PATH")?)
);
Ok(())
}

#[test]
fn test_tera_get_env_default() -> Result<(), Box<dyn std::error::Error>> {
let template = r#"shell: {{ get_env(name="FLOKI_TEST_UNSET", default="bar") }}"#;
assert_eq!(
render_template(template, Path::new("floki.yaml"))?,
"shell: bar"
);
Ok(())
}

#[test]
fn test_tera_get_env_missing_errors() {
let template = r#"shell: {{ get_env(name="FLOKI_TEST_UNSET") }}"#;
assert!(render_template(template, Path::new("floki.yaml")).is_err());
}

#[test]
fn test_strip_yaml_tags_drops_reference_tag() {
let yaml = "value: !reference [template, script]";
Expand Down
Loading