@@ -141,23 +141,11 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
141
141
}
142
142
}
143
143
144
- let mut get_env: Option < HashMap < String , String > > = None ;
145
- let mut get_input: Option < & str > = None ;
146
144
let args = process_args ( & resource. get . args , desired) ;
147
- match & resource. get . input {
148
- Some ( InputKind :: Env ) => {
149
- get_env = Some ( json_to_hashmap ( desired) ?) ;
150
- } ,
151
- Some ( InputKind :: Stdin ) => {
152
- get_input = Some ( desired) ;
153
- } ,
154
- None => {
155
- // leave input as none
156
- } ,
157
- }
145
+ let ( get_env, get_input) = get_env_stdin ( & resource. get . input , desired) ?;
158
146
159
147
info ! ( "Getting current state for set by invoking get {} using {}" , & resource. resource_type, & resource. get. executable) ;
160
- let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, get_input, Some ( cwd) , get_env) ?;
148
+ let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, get_input. as_deref ( ) , Some ( cwd) , get_env) ?;
161
149
log_resource_traces ( & stderr) ;
162
150
if exit_code != 0 {
163
151
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -282,23 +270,11 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re
282
270
283
271
verify_json ( resource, cwd, expected) ?;
284
272
285
- let mut env: Option < HashMap < String , String > > = None ;
286
- let mut input_expected: Option < & str > = None ;
287
273
let args = process_args ( & test. args , expected) ;
288
- match & test. input {
289
- Some ( InputKind :: Env ) => {
290
- env = Some ( json_to_hashmap ( expected) ?) ;
291
- } ,
292
- Some ( InputKind :: Stdin ) => {
293
- input_expected = Some ( expected) ;
294
- } ,
295
- None => {
296
- // leave input as none
297
- } ,
298
- }
274
+ let ( env, input_expected) = get_env_stdin ( & test. input , expected) ?;
299
275
300
276
info ! ( "Invoking test '{}' using '{}'" , & resource. resource_type, & test. executable) ;
301
- let ( exit_code, stdout, stderr) = invoke_command ( & test. executable , args, input_expected, Some ( cwd) , env) ?;
277
+ let ( exit_code, stdout, stderr) = invoke_command ( & test. executable , args, input_expected. as_deref ( ) , Some ( cwd) , env) ?;
302
278
log_resource_traces ( & stderr) ;
303
279
if exit_code != 0 {
304
280
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -412,23 +388,11 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
412
388
413
389
verify_json ( resource, cwd, filter) ?;
414
390
415
- let mut env: Option < HashMap < String , String > > = None ;
416
- let mut input_filter: Option < & str > = None ;
417
391
let args = process_args ( & delete. args , filter) ;
418
- match & delete. input {
419
- Some ( InputKind :: Env ) => {
420
- env = Some ( json_to_hashmap ( filter) ?) ;
421
- } ,
422
- Some ( InputKind :: Stdin ) => {
423
- input_filter = Some ( filter) ;
424
- } ,
425
- None => {
426
- // leave input as none
427
- } ,
428
- }
392
+ let ( env, input_filter) = get_env_stdin ( & delete. input , filter) ?;
429
393
430
394
info ! ( "Invoking delete '{}' using '{}'" , & resource. resource_type, & delete. executable) ;
431
- let ( exit_code, _stdout, stderr) = invoke_command ( & delete. executable , args, input_filter, Some ( cwd) , env) ?;
395
+ let ( exit_code, _stdout, stderr) = invoke_command ( & delete. executable , args, input_filter. as_deref ( ) , Some ( cwd) , env) ?;
432
396
log_resource_traces ( & stderr) ;
433
397
if exit_code != 0 {
434
398
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -459,22 +423,11 @@ pub fn invoke_validate(resource: &ResourceManifest, cwd: &str, config: &str) ->
459
423
return Err ( DscError :: NotImplemented ( "validate" . to_string ( ) ) ) ;
460
424
} ;
461
425
462
- let mut env: Option < HashMap < String , String > > = None ;
463
- let mut input_config: Option < & str > = None ;
464
426
let args = process_args ( & validate. args , config) ;
465
- match & validate. input {
466
- Some ( InputKind :: Env ) => {
467
- env = Some ( json_to_hashmap ( config) ?) ;
468
- } ,
469
- Some ( InputKind :: Stdin ) => {
470
- input_config = Some ( config) ;
471
- } ,
472
- None => {
473
- // leave input as none
474
- } ,
475
- }
427
+ let ( env, input_config) = get_env_stdin ( & validate. input , config) ?;
476
428
477
- let ( exit_code, stdout, stderr) = invoke_command ( & validate. executable , args, input_config, Some ( cwd) , env) ?;
429
+ info ! ( "Invoking validate '{}' using '{}'" , & resource. resource_type, & validate. executable) ;
430
+ let ( exit_code, stdout, stderr) = invoke_command ( & validate. executable , args, input_config. as_deref ( ) , Some ( cwd) , env) ?;
478
431
log_resource_traces ( & stderr) ;
479
432
if exit_code != 0 {
480
433
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -548,30 +501,20 @@ pub fn invoke_export(resource: &ResourceManifest, cwd: &str, input: Option<&str>
548
501
549
502
550
503
let mut env: Option < HashMap < String , String > > = None ;
551
- let mut export_input: Option < & str > = None ;
504
+ let mut export_input: Option < String > = None ;
552
505
let args: Option < Vec < String > > ;
553
506
if let Some ( input) = input {
554
507
if !input. is_empty ( ) {
555
508
verify_json ( resource, cwd, input) ?;
556
- match & export. input {
557
- Some ( InputKind :: Env ) => {
558
- env = Some ( json_to_hashmap ( input) ?) ;
559
- } ,
560
- Some ( InputKind :: Stdin ) => {
561
- export_input = Some ( input) ;
562
- } ,
563
- None => {
564
- // leave input as none
565
- } ,
566
- }
509
+ ( env, export_input) = get_env_stdin ( & export. input , input) ?;
567
510
}
568
511
569
512
args = process_args ( & export. args , input) ;
570
513
} else {
571
514
args = process_args ( & export. args , "" ) ;
572
515
}
573
516
574
- let ( exit_code, stdout, stderr) = invoke_command ( & export. executable , args, export_input, Some ( cwd) , env) ?;
517
+ let ( exit_code, stdout, stderr) = invoke_command ( & export. executable , args, export_input. as_deref ( ) , Some ( cwd) , env) ?;
575
518
log_resource_traces ( & stderr) ;
576
519
if exit_code != 0 {
577
520
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -695,6 +638,24 @@ fn process_args(args: &Option<Vec<ArgKind>>, value: &str) -> Option<Vec<String>>
695
638
Some ( processed_args)
696
639
}
697
640
641
+ fn get_env_stdin ( input_kind : & Option < InputKind > , input : & str ) -> Result < ( Option < HashMap < String , String > > , Option < String > ) , DscError > {
642
+ let mut env: Option < HashMap < String , String > > = None ;
643
+ let mut input_value: Option < String > = None ;
644
+ match input_kind {
645
+ Some ( InputKind :: Env ) => {
646
+ env = Some ( json_to_hashmap ( input) ?) ;
647
+ } ,
648
+ Some ( InputKind :: Stdin ) => {
649
+ input_value = Some ( input. to_string ( ) ) ;
650
+ } ,
651
+ None => {
652
+ // leave input as none
653
+ } ,
654
+ }
655
+
656
+ Ok ( ( env, input_value) )
657
+ }
658
+
698
659
fn verify_json ( resource : & ResourceManifest , cwd : & str , json : & str ) -> Result < ( ) , DscError > {
699
660
700
661
debug ! ( "Verify JSON for '{}'" , resource. resource_type) ;
0 commit comments