@@ -6,6 +6,7 @@ use crate::TEdgeConfig;
6
6
use crate :: TEdgeConfigDto ;
7
7
use crate :: TEdgeConfigError ;
8
8
use crate :: TEdgeConfigReader ;
9
+ use anyhow:: Context ;
9
10
use camino:: Utf8Path ;
10
11
use camino:: Utf8PathBuf ;
11
12
use serde:: Deserialize as _;
@@ -183,7 +184,7 @@ impl TEdgeConfigLocation {
183
184
}
184
185
185
186
if Sources :: INCLUDE_ENVIRONMENT {
186
- update_with_environment_variables ( & mut dto, & mut warnings) ;
187
+ update_with_environment_variables ( & mut dto, & mut warnings) ? ;
187
188
}
188
189
189
190
Ok ( ( dto, warnings) )
@@ -218,7 +219,7 @@ impl TEdgeConfigLocation {
218
219
}
219
220
220
221
if Sources :: INCLUDE_ENVIRONMENT {
221
- update_with_environment_variables ( & mut dto, & mut warnings) ;
222
+ update_with_environment_variables ( & mut dto, & mut warnings) ? ;
222
223
}
223
224
224
225
Ok ( ( dto, warnings) )
@@ -309,7 +310,10 @@ impl UnusedValueWarnings {
309
310
}
310
311
}
311
312
312
- fn update_with_environment_variables ( dto : & mut TEdgeConfigDto , warnings : & mut UnusedValueWarnings ) {
313
+ fn update_with_environment_variables (
314
+ dto : & mut TEdgeConfigDto ,
315
+ warnings : & mut UnusedValueWarnings ,
316
+ ) -> anyhow:: Result < ( ) > {
313
317
for ( key, value) in std:: env:: vars ( ) {
314
318
let tedge_key = match key. strip_prefix ( "TEDGE_" ) {
315
319
Some ( "CONFIG_DIR" ) => continue ,
@@ -344,13 +348,16 @@ fn update_with_environment_variables(dto: &mut TEdgeConfigDto, warnings: &mut Un
344
348
}
345
349
}
346
350
if value. is_empty ( ) {
347
- if let Err ( e) = dto. try_unset_key ( & tedge_key) {
348
- warnings. push ( format ! ( "Failed to process {key}: {e}" ) )
349
- }
350
- } else if let Err ( e) = dto. try_update_str ( & tedge_key, & value) {
351
- warnings. push ( format ! ( "Failed to process {key}: {e}" ) )
351
+ dto. try_unset_key ( & tedge_key) . with_context ( || {
352
+ format ! ( "Failed to reset value for {tedge_key} from environment variable {key}" )
353
+ } ) ?;
354
+ } else {
355
+ dto. try_update_str ( & tedge_key, & value) . with_context ( || {
356
+ format ! ( "Failed to set value for {tedge_key} to {value:?} from environment variable {key}" )
357
+ } ) ?;
352
358
}
353
359
}
360
+ Ok ( ( ) )
354
361
}
355
362
356
363
fn deserialize_toml (
@@ -679,6 +686,30 @@ type = "a-service-type""#;
679
686
assert_eq ! ( warnings. 0 , [ "Unknown configuration field \" unknown_value\" from environment variable TEDGE_UNKNOWN_VALUE" ] ) ;
680
687
}
681
688
689
+ #[ tokio:: test]
690
+ async fn unsetting_configuration_for_unknown_profile_does_not_warn_or_error ( ) {
691
+ let ( _dir, t) = create_temp_tedge_config ( "" ) . unwrap ( ) ;
692
+ let mut env = EnvSandbox :: new ( ) . await ;
693
+ env. set_var ( "TEDGE_C8Y_PROFILES_TEST_URL" , "" ) ;
694
+ let ( _config, warnings) = t
695
+ . load_dto_with_warnings :: < FileAndEnvironment > ( )
696
+ . await
697
+ . unwrap ( ) ;
698
+ assert_eq ! ( warnings. 0 , & [ ] as & [ & ' static str ] ) ;
699
+ }
700
+
701
+ #[ tokio:: test]
702
+ async fn environment_variable_causes_error_if_its_value_cannot_be_parsed ( ) {
703
+ let ( _dir, t) = create_temp_tedge_config ( "" ) . unwrap ( ) ;
704
+ let mut env = EnvSandbox :: new ( ) . await ;
705
+ env. set_var ( "TEDGE_SUDO_ENABLE" , "yes" ) ;
706
+ let err = t
707
+ . load_dto_with_warnings :: < FileAndEnvironment > ( )
708
+ . await
709
+ . unwrap_err ( ) ;
710
+ assert ! ( dbg!( err. to_string( ) ) . contains( "TEDGE_SUDO_ENABLE" ) ) ;
711
+ }
712
+
682
713
#[ tokio:: test]
683
714
async fn environment_variables_are_ignored_in_file_only_mode ( ) {
684
715
let ( _dir, t) = create_temp_tedge_config ( "sudo.enable = true" ) . unwrap ( ) ;
0 commit comments