@@ -195,6 +195,20 @@ fn makeloader(
195195 }
196196}
197197
198+ // Reimplementation of tera 1's `get_env`, which was dropped in tera 2.
199+ fn get_env ( kwargs : tera:: Kwargs , _: & tera:: State ) -> tera:: TeraResult < tera:: Value > {
200+ let name: String = kwargs. must_get ( "name" ) ?;
201+ match std:: env:: var ( & name) {
202+ Ok ( value) => Ok ( value. into ( ) ) ,
203+ Err ( _) => match kwargs. get :: < tera:: Value > ( "default" ) ? {
204+ Some ( default) => Ok ( default) ,
205+ None => Err ( tera:: Error :: message ( format ! (
206+ "Environment variable `{name}` not found"
207+ ) ) ) ,
208+ } ,
209+ }
210+ }
211+
198212// Renders a template from a given string.
199213pub fn render_template ( template : & str , source_filename : & Path ) -> Result < String , FlokiError > {
200214 let template_path = source_filename. display ( ) . to_string ( ) ;
@@ -216,6 +230,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
216230 tera. register_function ( "yaml" , makeloader ( & canonical_path, LoaderType :: Yaml ) ) ;
217231 tera. register_function ( "json" , makeloader ( & canonical_path, LoaderType :: Json ) ) ;
218232 tera. register_function ( "toml" , makeloader ( & canonical_path, LoaderType :: Toml ) ) ;
233+ tera. register_function ( "get_env" , get_env) ;
219234
220235 tera. add_raw_template ( & template_path, template)
221236 . map_err ( |e| FlokiError :: ProblemRenderingTemplate {
@@ -413,6 +428,34 @@ mod test {
413428 Ok ( ( ) )
414429 }
415430
431+ #[ test]
432+ fn test_tera_get_env ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
433+ // Uses PATH rather than setting a var, as set_var is unsound in a threaded
434+ // test binary.
435+ let template = r#"shell: {{ get_env(name="PATH") }}"# ;
436+ assert_eq ! (
437+ render_template( template, Path :: new( "floki.yaml" ) ) ?,
438+ format!( "shell: {}" , std:: env:: var( "PATH" ) ?)
439+ ) ;
440+ Ok ( ( ) )
441+ }
442+
443+ #[ test]
444+ fn test_tera_get_env_default ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
445+ let template = r#"shell: {{ get_env(name="FLOKI_TEST_UNSET", default="bar") }}"# ;
446+ assert_eq ! (
447+ render_template( template, Path :: new( "floki.yaml" ) ) ?,
448+ "shell: bar"
449+ ) ;
450+ Ok ( ( ) )
451+ }
452+
453+ #[ test]
454+ fn test_tera_get_env_missing_errors ( ) {
455+ let template = r#"shell: {{ get_env(name="FLOKI_TEST_UNSET") }}"# ;
456+ assert ! ( render_template( template, Path :: new( "floki.yaml" ) ) . is_err( ) ) ;
457+ }
458+
416459 #[ test]
417460 fn test_strip_yaml_tags_drops_reference_tag ( ) {
418461 let yaml = "value: !reference [template, script]" ;
0 commit comments