@@ -45,31 +45,16 @@ pub fn log_resource_traces(stderr: &str)
45
45
///
46
46
/// Error returned if the resource does not successfully get the current state
47
47
pub fn invoke_get ( resource : & ResourceManifest , cwd : & str , filter : & str ) -> Result < GetResult , DscError > {
48
- let input_kind = if let Some ( input_kind) = & resource. get . input {
49
- input_kind. clone ( )
50
- }
51
- else {
52
- InputKind :: Stdin
53
- } ;
54
-
55
- let mut env: Option < HashMap < String , String > > = None ;
56
- let mut input_filter: Option < & str > = None ;
48
+ let mut command_input = CommandInput { env : None , stdin : None } ;
57
49
let args = process_args ( & resource. get . args , filter) ;
58
50
if !filter. is_empty ( ) {
59
51
verify_json ( resource, cwd, filter) ?;
60
52
61
- match input_kind {
62
- InputKind :: Env => {
63
- env = Some ( json_to_hashmap ( filter) ?) ;
64
- } ,
65
- InputKind :: Stdin => {
66
- input_filter = Some ( filter) ;
67
- } ,
68
- }
53
+ command_input = get_command_input ( & resource. get . input , filter) ?;
69
54
}
70
55
71
56
info ! ( "Invoking get '{}' using '{}'" , & resource. resource_type, & resource. get. executable) ;
72
- let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, input_filter , Some ( cwd) , env) ?;
57
+ let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
73
58
log_resource_traces ( & stderr) ;
74
59
if exit_code != 0 {
75
60
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -142,10 +127,10 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
142
127
}
143
128
144
129
let args = process_args ( & resource. get . args , desired) ;
145
- let ( get_env , get_input ) = get_env_stdin ( & resource. get . input , desired) ?;
130
+ let command_input = get_command_input ( & resource. get . input , desired) ?;
146
131
147
132
info ! ( "Getting current state for set by invoking get {} using {}" , & resource. resource_type, & resource. get. executable) ;
148
- let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, get_input . as_deref ( ) , Some ( cwd) , get_env ) ?;
133
+ let ( exit_code, stdout, stderr) = invoke_command ( & resource. get . executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
149
134
log_resource_traces ( & stderr) ;
150
135
if exit_code != 0 {
151
136
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -271,10 +256,10 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re
271
256
verify_json ( resource, cwd, expected) ?;
272
257
273
258
let args = process_args ( & test. args , expected) ;
274
- let ( env , input_expected ) = get_env_stdin ( & test. input , expected) ?;
259
+ let command_input = get_command_input ( & test. input , expected) ?;
275
260
276
261
info ! ( "Invoking test '{}' using '{}'" , & resource. resource_type, & test. executable) ;
277
- let ( exit_code, stdout, stderr) = invoke_command ( & test. executable , args, input_expected . as_deref ( ) , Some ( cwd) , env) ?;
262
+ let ( exit_code, stdout, stderr) = invoke_command ( & test. executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
278
263
log_resource_traces ( & stderr) ;
279
264
if exit_code != 0 {
280
265
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -389,10 +374,10 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
389
374
verify_json ( resource, cwd, filter) ?;
390
375
391
376
let args = process_args ( & delete. args , filter) ;
392
- let ( env , input_filter ) = get_env_stdin ( & delete. input , filter) ?;
377
+ let command_input = get_command_input ( & delete. input , filter) ?;
393
378
394
379
info ! ( "Invoking delete '{}' using '{}'" , & resource. resource_type, & delete. executable) ;
395
- let ( exit_code, _stdout, stderr) = invoke_command ( & delete. executable , args, input_filter . as_deref ( ) , Some ( cwd) , env) ?;
380
+ let ( exit_code, _stdout, stderr) = invoke_command ( & delete. executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
396
381
log_resource_traces ( & stderr) ;
397
382
if exit_code != 0 {
398
383
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -424,10 +409,10 @@ pub fn invoke_validate(resource: &ResourceManifest, cwd: &str, config: &str) ->
424
409
} ;
425
410
426
411
let args = process_args ( & validate. args , config) ;
427
- let ( env , input_config ) = get_env_stdin ( & validate. input , config) ?;
412
+ let command_input = get_command_input ( & validate. input , config) ?;
428
413
429
414
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) ?;
415
+ let ( exit_code, stdout, stderr) = invoke_command ( & validate. executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
431
416
log_resource_traces ( & stderr) ;
432
417
if exit_code != 0 {
433
418
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -500,21 +485,21 @@ pub fn invoke_export(resource: &ResourceManifest, cwd: &str, input: Option<&str>
500
485
} ;
501
486
502
487
503
- let mut env: Option < HashMap < String , String > > = None ;
504
- let mut export_input: Option < String > = None ;
488
+ let mut command_input: CommandInput = CommandInput { env : None , stdin : None } ;
505
489
let args: Option < Vec < String > > ;
506
490
if let Some ( input) = input {
507
491
if !input. is_empty ( ) {
508
492
verify_json ( resource, cwd, input) ?;
509
- ( env, export_input) = get_env_stdin ( & export. input , input) ?;
493
+
494
+ command_input = get_command_input ( & export. input , input) ?;
510
495
}
511
496
512
497
args = process_args ( & export. args , input) ;
513
498
} else {
514
499
args = process_args ( & export. args , "" ) ;
515
500
}
516
501
517
- let ( exit_code, stdout, stderr) = invoke_command ( & export. executable , args, export_input . as_deref ( ) , Some ( cwd) , env) ?;
502
+ let ( exit_code, stdout, stderr) = invoke_command ( & export. executable , args, command_input . stdin . as_deref ( ) , Some ( cwd) , command_input . env ) ?;
518
503
log_resource_traces ( & stderr) ;
519
504
if exit_code != 0 {
520
505
return Err ( DscError :: Command ( resource. resource_type . clone ( ) , exit_code, stderr) ) ;
@@ -638,22 +623,30 @@ fn process_args(args: &Option<Vec<ArgKind>>, value: &str) -> Option<Vec<String>>
638
623
Some ( processed_args)
639
624
}
640
625
641
- fn get_env_stdin ( input_kind : & Option < InputKind > , input : & str ) -> Result < ( Option < HashMap < String , String > > , Option < String > ) , DscError > {
626
+ struct CommandInput {
627
+ env : Option < HashMap < String , String > > ,
628
+ stdin : Option < String > ,
629
+ }
630
+
631
+ fn get_command_input ( input_kind : & Option < InputKind > , input : & str ) -> Result < CommandInput , DscError > {
642
632
let mut env: Option < HashMap < String , String > > = None ;
643
- let mut input_value : Option < String > = None ;
633
+ let mut stdin : Option < String > = None ;
644
634
match input_kind {
645
635
Some ( InputKind :: Env ) => {
646
636
env = Some ( json_to_hashmap ( input) ?) ;
647
637
} ,
648
638
Some ( InputKind :: Stdin ) => {
649
- input_value = Some ( input. to_string ( ) ) ;
639
+ stdin = Some ( input. to_string ( ) ) ;
650
640
} ,
651
641
None => {
652
642
// leave input as none
653
643
} ,
654
644
}
655
645
656
- Ok ( ( env, input_value) )
646
+ Ok ( CommandInput {
647
+ env,
648
+ stdin,
649
+ } )
657
650
}
658
651
659
652
fn verify_json ( resource : & ResourceManifest , cwd : & str , json : & str ) -> Result < ( ) , DscError > {
0 commit comments