Skip to content

Commit cd13485

Browse files
authored
Merge pull request #862 from SteveL-MSFT/resource-pass-through
Add `pass-through` output format for `dsc resource get`
2 parents b4406cf + c678d8e commit cd13485

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

dsc/src/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum OutputFormat {
1919
pub enum GetOutputFormat {
2020
Json,
2121
JsonArray,
22+
PassThrough,
2223
PrettyJson,
2324
Yaml,
2425
}

dsc/src/resource_command.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use dsc_lib::{
1616
};
1717
use std::process::exit;
1818

19-
pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&OutputFormat>) {
19+
pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&GetOutputFormat>) {
2020
let Some(resource) = get_resource(dsc, resource_type) else {
2121
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
2222
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
@@ -30,18 +30,37 @@ pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
3030

3131
match resource.get(input) {
3232
Ok(result) => {
33+
if let GetResult::Resource(response) = &result {
34+
if format == Some(&GetOutputFormat::PassThrough) {
35+
let json = match serde_json::to_string(&response.actual_state) {
36+
Ok(json) => json,
37+
Err(err) => {
38+
error!("{}", t!("resource_command.jsonError", err = err));
39+
exit(EXIT_JSON_ERROR);
40+
}
41+
};
42+
write_object(&json, Some(&OutputFormat::Json), false);
43+
return;
44+
}
45+
}
46+
3347
// convert to json
3448
let json = match serde_json::to_string(&result) {
3549
Ok(json) => json,
3650
Err(err) => {
37-
error!("JSON Error: {err}");
51+
error!("{}", t!("resource_command.jsonError", err = err));
3852
exit(EXIT_JSON_ERROR);
3953
}
4054
};
55+
let format = match format {
56+
Some(&GetOutputFormat::PrettyJson) => Some(&OutputFormat::PrettyJson),
57+
Some(&GetOutputFormat::Yaml) => Some(&OutputFormat::Yaml),
58+
_ => Some(&OutputFormat::Json),
59+
};
4160
write_object(&json, format, false);
4261
}
4362
Err(err) => {
44-
error!("Error: {err}");
63+
error!("{err}");
4564
exit(EXIT_DSC_ERROR);
4665
}
4766
}
@@ -63,7 +82,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&GetOutputF
6382
let export_result = match resource.export(&input) {
6483
Ok(export) => { export }
6584
Err(err) => {
66-
error!("Error: {err}");
85+
error!("{err}");
6786
exit(EXIT_DSC_ERROR);
6887
}
6988
};
@@ -127,14 +146,14 @@ pub fn set(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
127146
let json = match serde_json::to_string(&result) {
128147
Ok(json) => json,
129148
Err(err) => {
130-
error!("JSON Error: {err}");
149+
error!("{}", t!("resource_command.jsonError", err = err));
131150
exit(EXIT_JSON_ERROR);
132151
}
133152
};
134153
write_object(&json, format, false);
135154
}
136155
Err(err) => {
137-
error!("Error: {err}");
156+
error!("{err}");
138157
exit(EXIT_DSC_ERROR);
139158
}
140159
}
@@ -191,7 +210,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, input: &str) {
191210
match resource.delete(input) {
192211
Ok(()) => {}
193212
Err(err) => {
194-
error!("Error: {err}");
213+
error!("{err}");
195214
exit(EXIT_DSC_ERROR);
196215
}
197216
}
@@ -213,14 +232,14 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
213232
match serde_json::from_str::<serde_json::Value>(json.as_str()) {
214233
Ok(_) => (),
215234
Err(err) => {
216-
error!("Error: {err}");
235+
error!("{err}");
217236
exit(EXIT_JSON_ERROR);
218237
}
219238
}
220239
write_object(&json, format, false);
221240
}
222241
Err(err) => {
223-
error!("Error: {err}");
242+
error!("{err}");
224243
exit(EXIT_DSC_ERROR);
225244
}
226245
}

dsc/src/subcommand.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -588,20 +588,16 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
588588
},
589589
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
590590
dsc.find_resources(&[resource.to_string()], progress_format);
591-
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
591+
if *all {
592+
resource_command::get_all(&dsc, resource, output_format.as_ref());
593+
}
592594
else {
595+
if *output_format == Some(GetOutputFormat::JsonArray) {
596+
error!("{}", t!("subcommand.jsonArrayNotSupported"));
597+
exit(EXIT_INVALID_ARGS);
598+
}
593599
let parsed_input = get_input(input.as_ref(), path.as_ref(), false);
594-
let format = match output_format {
595-
Some(GetOutputFormat::Json) => Some(OutputFormat::Json),
596-
Some(GetOutputFormat::JsonArray) => {
597-
error!("{}", t!("subcommand.jsonArrayNotSupported"));
598-
exit(EXIT_INVALID_ARGS);
599-
},
600-
Some(GetOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
601-
Some(GetOutputFormat::Yaml) => Some(OutputFormat::Yaml),
602-
None => None,
603-
};
604-
resource_command::get(&dsc, resource, &parsed_input, format.as_ref());
600+
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
605601
}
606602
},
607603
ResourceSubCommand::Set { resource, input, file: path, output_format } => {

dsc/tests/dsc_resource_get.tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ Describe 'resource get tests' {
5454
$LASTEXITCODE | Should -Be 0
5555
($out | Measure-Object).Count | Should -Be 1
5656
}
57+
58+
It 'pass-through format works' {
59+
$out = dsc resource get -r Microsoft/OSInfo --output-format pass-through | ConvertFrom-Json
60+
$LASTEXITCODE | Should -Be 0
61+
$expectedFamily = if ($IsWindows) {
62+
'Windows'
63+
} elseif ($IsLinux) {
64+
'Linux'
65+
} else {
66+
'macOS'
67+
}
68+
$out.family | Should -BeExactly $expectedFamily
69+
$out.version | Should -Not -BeNullOrEmpty
70+
$out.bitness | Should -BeIn @('32', '64')
71+
$out.architecture | Should -BeIn @('x86', 'x86_64', 'arm64')
72+
}
5773
}

0 commit comments

Comments
 (0)