Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dsc/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ implementedAs = "implemented as"
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
setInputEmpty = "Desired input is empty"
testInputEmpty = "Expected input is required"
jsonError = "JSON: %{err}"

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

[util]
failedToConvertJsonToString = "Failed to convert JSON to string"
Expand Down
10 changes: 9 additions & 1 deletion dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub enum OutputFormat {
Yaml,
}

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum GetOutputFormat {
Json,
JsonArray,
PrettyJson,
Yaml,
}

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum ListOutputFormat {
Json,
Expand Down Expand Up @@ -195,7 +203,7 @@ pub enum ResourceSubCommand {
#[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")]
file: Option<String>,
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
output_format: Option<OutputFormat>,
output_format: Option<GetOutputFormat>,
},
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
Set {
Expand Down
23 changes: 20 additions & 3 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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

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

if format == Some(&GetOutputFormat::JsonArray) {
let json = match serde_json::to_string(&export_result.actual_state) {
Ok(json) => json,
Err(err) => {
error!("{}", t!("resource_command.jsonError", err = err));
exit(EXIT_JSON_ERROR);
}
};
write_object(&json, Some(&OutputFormat::Json), false);
return;
}

let mut include_separator = false;
for instance in export_result.actual_state
{
Expand All @@ -78,10 +90,15 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
let json = match serde_json::to_string(&get_result) {
Ok(json) => json,
Err(err) => {
error!("JSON Error: {err}");
error!("{}", t!("resource_command.jsonError", err = err));
exit(EXIT_JSON_ERROR);
}
};
let format = match format {
Some(&GetOutputFormat::PrettyJson) => Some(&OutputFormat::PrettyJson),
Some(&GetOutputFormat::Yaml) => Some(&OutputFormat::Yaml),
_ => Some(&OutputFormat::Json),
};
write_object(&json, format, include_separator);
include_separator = true;
}
Expand Down
14 changes: 12 additions & 2 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, ListOutputFormat, OutputFormat, ResourceSubCommand};
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, GetOutputFormat, ListOutputFormat, OutputFormat, ResourceSubCommand};
use crate::resolve::{get_contents, Include};
use crate::resource_command::{get_resource, self};
use crate::tablewriter::Table;
Expand Down Expand Up @@ -590,7 +590,17 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
else {
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
let format = match output_format {
Some(GetOutputFormat::Json) => Some(OutputFormat::Json),
Some(GetOutputFormat::JsonArray) => {
error!("{}", t!("subcommand.jsonArrayNotSupported"));
exit(EXIT_INVALID_ARGS);
},
Some(GetOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
Some(GetOutputFormat::Yaml) => Some(OutputFormat::Yaml),
None => None,
};
resource_command::get(&dsc, resource, &parsed_input, format.as_ref());
}
},
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ Describe 'resource get tests' {
$testError[0] | SHould -match 'error:'
$LASTEXITCODE | Should -Be 2
}

It '--output-format json-array returns single object' {
$out = dsc resource get -r Microsoft/Process --all --output-format json-array
$LASTEXITCODE | Should -Be 0
($out | Measure-Object).Count | Should -Be 1
}
}
Loading