@@ -195,7 +195,21 @@ impl<'a> FunctionRef<'a> {
195195 . as_str ( ) ;
196196 let var_default = caps. get ( 3 ) . map ( |m| m. as_str ( ) ) ;
197197
198- if let Some ( value) = arguments. get ( var_name) {
198+ // read variable from the environment if the name starts with env. or ENV.
199+ if var_name. starts_with ( "env." ) || var_name. starts_with ( "ENV." ) {
200+ let env_var_name = var_name. replace ( "env." , "" ) . replace ( "ENV." , "" ) ;
201+ let env_var = std:: env:: var ( & env_var_name) ;
202+ if let Ok ( value) = env_var {
203+ value
204+ } else if var_default. is_some ( ) {
205+ var_default. unwrap ( ) . to_string ( )
206+ } else {
207+ return Err ( anyhow:: anyhow!(
208+ "environment variable {} not set" ,
209+ env_var_name
210+ ) ) ;
211+ }
212+ } else if let Some ( value) = arguments. get ( var_name) {
199213 // if the value is empty and there's a default value, use the default value
200214 if value. is_empty ( ) && var_default. is_some ( ) {
201215 var_default. unwrap ( ) . to_string ( )
@@ -386,4 +400,103 @@ mod tests {
386400 assert ! ( command_line. app. ends_with( "/echo" ) ) ;
387401 assert_eq ! ( command_line. args, vec![ "value1" , "default" , "literal" ] ) ;
388402 }
403+
404+ #[ test]
405+ fn test_resolve_command_line_with_env_variables ( ) {
406+ std:: env:: set_var ( "TEST_VAR" , "test_value" ) ;
407+
408+ let function = Function {
409+ execution : ExecutionContext :: CommandLine ( vec ! [
410+ "echo" . to_string( ) ,
411+ "${env.TEST_VAR}" . to_string( ) ,
412+ "${ENV.TEST_VAR}" . to_string( ) ,
413+ ] ) ,
414+ description : "" . to_string ( ) ,
415+ parameters : BTreeMap :: new ( ) ,
416+ container : None ,
417+ } ;
418+ let resolver = FunctionRef {
419+ function : & function,
420+ name : "test_function" . to_string ( ) ,
421+ path : & Utf8PathBuf :: from ( "test/path" ) ,
422+ page : & Page {
423+ name : "test_page" . to_string ( ) ,
424+ description : None ,
425+ categories : Vec :: new ( ) ,
426+ functions : BTreeMap :: new ( ) ,
427+ } ,
428+ } ;
429+ let arguments = BTreeMap :: new ( ) ;
430+
431+ let result = resolver. resolve_command_line ( & arguments) ;
432+ assert ! ( result. is_ok( ) ) ;
433+ let command_line = result. unwrap ( ) ;
434+ assert ! ( command_line. app. ends_with( "/echo" ) ) ;
435+ assert_eq ! ( command_line. args, vec![ "test_value" , "test_value" ] ) ;
436+
437+ std:: env:: remove_var ( "TEST_VAR" ) ;
438+ }
439+
440+ #[ test]
441+ fn test_resolve_command_line_with_undefined_env_variable ( ) {
442+ let function = Function {
443+ execution : ExecutionContext :: CommandLine ( vec ! [
444+ "echo" . to_string( ) ,
445+ "${env.UNDEFINED_VAR}" . to_string( ) ,
446+ ] ) ,
447+ description : "" . to_string ( ) ,
448+ parameters : BTreeMap :: new ( ) ,
449+ container : None ,
450+ } ;
451+ let resolver = FunctionRef {
452+ function : & function,
453+ name : "test_function" . to_string ( ) ,
454+ path : & Utf8PathBuf :: from ( "test/path" ) ,
455+ page : & Page {
456+ name : "test_page" . to_string ( ) ,
457+ description : None ,
458+ categories : Vec :: new ( ) ,
459+ functions : BTreeMap :: new ( ) ,
460+ } ,
461+ } ;
462+ let arguments = BTreeMap :: new ( ) ;
463+
464+ let result = resolver. resolve_command_line ( & arguments) ;
465+ assert ! ( result. is_err( ) ) ;
466+ assert_eq ! (
467+ result. unwrap_err( ) . to_string( ) ,
468+ "environment variable UNDEFINED_VAR not set"
469+ ) ;
470+ }
471+
472+ #[ test]
473+ fn test_resolve_command_line_with_undefined_env_variable_with_default ( ) {
474+ let function = Function {
475+ execution : ExecutionContext :: CommandLine ( vec ! [
476+ "echo" . to_string( ) ,
477+ "${env.UNDEFINED_VAR or default_value}" . to_string( ) ,
478+ ] ) ,
479+ description : "" . to_string ( ) ,
480+ parameters : BTreeMap :: new ( ) ,
481+ container : None ,
482+ } ;
483+ let resolver = FunctionRef {
484+ function : & function,
485+ name : "test_function" . to_string ( ) ,
486+ path : & Utf8PathBuf :: from ( "test/path" ) ,
487+ page : & Page {
488+ name : "test_page" . to_string ( ) ,
489+ description : None ,
490+ categories : Vec :: new ( ) ,
491+ functions : BTreeMap :: new ( ) ,
492+ } ,
493+ } ;
494+ let arguments = BTreeMap :: new ( ) ;
495+
496+ let result = resolver. resolve_command_line ( & arguments) ;
497+ assert ! ( result. is_ok( ) ) ;
498+ let command_line = result. unwrap ( ) ;
499+ assert ! ( command_line. app. ends_with( "/echo" ) ) ;
500+ assert_eq ! ( command_line. args, vec![ "default_value" ] ) ;
501+ }
389502}
0 commit comments