Skip to content

Commit 40389df

Browse files
author
Max Dymond
committed
Add tomlload function to tera templating engine
Floki templates can use `tomlload(file="<filepath>")` in order to load values. Signed-off-by: Max Dymond <max.dymond@microsoft.com>
1 parent b534214 commit 40389df

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Status: Available for use
1818
can use `yamlload(file="<filepath>")` in order to load values.
1919
- Add `jsonload` function to tera templating engine so that floki templates
2020
can use `jsonload(file="<filepath>")` in order to load values.
21+
- Add `tomlload` function to tera templating engine so that floki templates
22+
can use `tomlload(file="<filepath>")` in order to load values.
2123

2224
### Fixed
2325

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ anyhow = "1.0.71"
3030
thiserror = "1.0.40"
3131
tera = "1"
3232
serde_json = "1.0.100"
33+
toml = "0.5.11"
3334

3435
[dev-dependencies]
3536
tempfile = "3.6.0"

src/config.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,28 +99,32 @@ pub(crate) struct FlokiConfig {
9999
pub(crate) entrypoint: Entrypoint,
100100
}
101101

102-
fn yamlloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
102+
fn path_from_args(args: &HashMap<String, tera::Value>) -> tera::Result<String> {
103103
let file = match args.get("file") {
104104
Some(file) => file,
105105
None => return Err("file parameter is required".into()),
106106
};
107+
Ok(from_value::<String>(file.clone())?)
108+
}
107109

108-
let path = from_value::<String>(file.clone())?;
110+
fn yamlloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
111+
let path = path_from_args(args)?;
109112
let f = std::fs::File::open(path)?;
110113
serde_yaml::from_reader(f).map_err(|_| "Failed to read file".into())
111114
}
112115

113116
fn jsonloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
114-
let file = match args.get("file") {
115-
Some(file) => file,
116-
None => return Err("file parameter is required".into()),
117-
};
118-
119-
let path = from_value::<String>(file.clone())?;
117+
let path = path_from_args(args)?;
120118
let f = std::fs::File::open(path)?;
121119
serde_json::from_reader(f).map_err(|_| "Failed to read file".into())
122120
}
123121

122+
fn tomlloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
123+
let path = path_from_args(args)?;
124+
let contents = std::fs::read_to_string(path)?;
125+
toml::from_str(&contents).map_err(|_| "Failed to read file".into())
126+
}
127+
124128
// Renders a template from a given string.
125129
pub fn render_template(template: &str, source_filename: &Path) -> Result<String, Error> {
126130
let template_path = source_filename.display().to_string();
@@ -130,9 +134,10 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
130134
// Read the template using tera
131135
let mut tera = Tera::default();
132136

133-
// Allow templates to load yaml and json files as Values.
137+
// Allow templates to load variables files as Values.
134138
tera.register_function("yamlload", yamlloader);
135139
tera.register_function("jsonload", jsonloader);
140+
tera.register_function("tomlload", tomlloader);
136141

137142
tera.add_raw_template(&template_path, template)
138143
.map_err(|e| errors::FlokiError::ProblemRenderingTemplate {
@@ -328,4 +333,13 @@ mod test {
328333
assert_eq!(config, "shell: bar");
329334
Ok(())
330335
}
336+
337+
#[test]
338+
fn test_tera_tomlload() -> Result<(), Box<dyn std::error::Error>> {
339+
let template =
340+
r#"{% set values = tomlload(file="Cargo.toml") %}floki: {{ values.package.name }}"#;
341+
let config = render_template(template, Path::new("floki"))?;
342+
assert_eq!(config, "floki: floki");
343+
Ok(())
344+
}
331345
}

0 commit comments

Comments
 (0)