Skip to content

Commit 07e26f9

Browse files
authored
Merge pull request #861 from SteveL-MSFT/get-all-array
Add `json-array` as supported format to `get --all`
2 parents da89748 + 5310013 commit 07e26f9

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

dsc/locales/en-us.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ implementedAs = "implemented as"
7373
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
7474
setInputEmpty = "Desired input is empty"
7575
testInputEmpty = "Expected input is required"
76+
jsonError = "JSON: %{err}"
7677

7778
[subcommand]
7879
actualStateNotObject = "actual_state is not an object"
@@ -108,6 +109,7 @@ tableHeader_capabilities = "Capabilities"
108109
tableHeader_adapter = "RequireAdapter"
109110
tableHeader_description = "Description"
110111
invalidManifest = "Error in manifest for"
112+
jsonArrayNotSupported = "JSON array output format is only supported for `--all'"
111113

112114
[util]
113115
failedToConvertJsonToString = "Failed to convert JSON to string"

dsc/src/args.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ pub enum OutputFormat {
1515
Yaml,
1616
}
1717

18+
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
19+
pub enum GetOutputFormat {
20+
Json,
21+
JsonArray,
22+
PrettyJson,
23+
Yaml,
24+
}
25+
1826
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
1927
pub enum ListOutputFormat {
2028
Json,
@@ -195,7 +203,7 @@ pub enum ResourceSubCommand {
195203
#[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")]
196204
file: Option<String>,
197205
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
198-
output_format: Option<OutputFormat>,
206+
output_format: Option<GetOutputFormat>,
199207
},
200208
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
201209
Set {

dsc/src/resource_command.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::args::OutputFormat;
4+
use crate::args::{GetOutputFormat, OutputFormat};
55
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, write_object};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
@@ -47,7 +47,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
4747
}
4848
}
4949

50-
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
50+
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&GetOutputFormat>) {
5151
let input = String::new();
5252
let Some(resource) = get_resource(dsc, resource_type) else {
5353
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
@@ -68,6 +68,18 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
6868
}
6969
};
7070

71+
if format == Some(&GetOutputFormat::JsonArray) {
72+
let json = match serde_json::to_string(&export_result.actual_state) {
73+
Ok(json) => json,
74+
Err(err) => {
75+
error!("{}", t!("resource_command.jsonError", err = err));
76+
exit(EXIT_JSON_ERROR);
77+
}
78+
};
79+
write_object(&json, Some(&OutputFormat::Json), false);
80+
return;
81+
}
82+
7183
let mut include_separator = false;
7284
for instance in export_result.actual_state
7385
{
@@ -78,10 +90,15 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
7890
let json = match serde_json::to_string(&get_result) {
7991
Ok(json) => json,
8092
Err(err) => {
81-
error!("JSON Error: {err}");
93+
error!("{}", t!("resource_command.jsonError", err = err));
8294
exit(EXIT_JSON_ERROR);
8395
}
8496
};
97+
let format = match format {
98+
Some(&GetOutputFormat::PrettyJson) => Some(&OutputFormat::PrettyJson),
99+
Some(&GetOutputFormat::Yaml) => Some(&OutputFormat::Yaml),
100+
_ => Some(&OutputFormat::Json),
101+
};
85102
write_object(&json, format, include_separator);
86103
include_separator = true;
87104
}

dsc/src/subcommand.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, ListOutputFormat, OutputFormat, ResourceSubCommand};
4+
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, GetOutputFormat, ListOutputFormat, OutputFormat, ResourceSubCommand};
55
use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
@@ -590,7 +590,17 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
590590
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
591591
else {
592592
let parsed_input = get_input(input.as_ref(), path.as_ref());
593-
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
593+
let format = match output_format {
594+
Some(GetOutputFormat::Json) => Some(OutputFormat::Json),
595+
Some(GetOutputFormat::JsonArray) => {
596+
error!("{}", t!("subcommand.jsonArrayNotSupported"));
597+
exit(EXIT_INVALID_ARGS);
598+
},
599+
Some(GetOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
600+
Some(GetOutputFormat::Yaml) => Some(OutputFormat::Yaml),
601+
None => None,
602+
};
603+
resource_command::get(&dsc, resource, &parsed_input, format.as_ref());
594604
}
595605
},
596606
ResourceSubCommand::Set { resource, input, file: path, output_format } => {

dsc/tests/dsc_get.tests.ps1 renamed to dsc/tests/dsc_resource_get.tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ Describe 'resource get tests' {
4848
$testError[0] | SHould -match 'error:'
4949
$LASTEXITCODE | Should -Be 2
5050
}
51+
52+
It '--output-format json-array returns single object' {
53+
$out = dsc resource get -r Microsoft/Process --all --output-format json-array
54+
$LASTEXITCODE | Should -Be 0
55+
($out | Measure-Object).Count | Should -Be 1
56+
}
5157
}

0 commit comments

Comments
 (0)