Skip to content

Commit ba80ec7

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
fix clippy
1 parent 6db080b commit ba80ec7

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,16 @@ pub fn log_resource_traces(stderr: &str)
4545
///
4646
/// Error returned if the resource does not successfully get the current state
4747
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 };
5749
let args = process_args(&resource.get.args, filter);
5850
if !filter.is_empty() {
5951
verify_json(resource, cwd, filter)?;
6052

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)?;
6954
}
7055

7156
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)?;
7358
log_resource_traces(&stderr);
7459
if exit_code != 0 {
7560
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
142127
}
143128

144129
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)?;
146131

147132
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)?;
149134
log_resource_traces(&stderr);
150135
if exit_code != 0 {
151136
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
271256
verify_json(resource, cwd, expected)?;
272257

273258
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)?;
275260

276261
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)?;
278263
log_resource_traces(&stderr);
279264
if exit_code != 0 {
280265
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
389374
verify_json(resource, cwd, filter)?;
390375

391376
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)?;
393378

394379
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)?;
396381
log_resource_traces(&stderr);
397382
if exit_code != 0 {
398383
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) ->
424409
};
425410

426411
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)?;
428413

429414
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)?;
431416
log_resource_traces(&stderr);
432417
if exit_code != 0 {
433418
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>
500485
};
501486

502487

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 };
505489
let args: Option<Vec<String>>;
506490
if let Some(input) = input {
507491
if !input.is_empty() {
508492
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)?;
510495
}
511496

512497
args = process_args(&export.args, input);
513498
} else {
514499
args = process_args(&export.args, "");
515500
}
516501

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)?;
518503
log_resource_traces(&stderr);
519504
if exit_code != 0 {
520505
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>>
638623
Some(processed_args)
639624
}
640625

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> {
642632
let mut env: Option<HashMap<String, String>> = None;
643-
let mut input_value: Option<String> = None;
633+
let mut stdin: Option<String> = None;
644634
match input_kind {
645635
Some(InputKind::Env) => {
646636
env = Some(json_to_hashmap(input)?);
647637
},
648638
Some(InputKind::Stdin) => {
649-
input_value = Some(input.to_string());
639+
stdin = Some(input.to_string());
650640
},
651641
None => {
652642
// leave input as none
653643
},
654644
}
655645

656-
Ok((env, input_value))
646+
Ok(CommandInput {
647+
env,
648+
stdin,
649+
})
657650
}
658651

659652
fn verify_json(resource: &ResourceManifest, cwd: &str, json: &str) -> Result<(), DscError> {

0 commit comments

Comments
 (0)