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
1 change: 1 addition & 0 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum OutputFormat {
pub enum GetOutputFormat {
Json,
JsonArray,
PassThrough,
PrettyJson,
Yaml,
}
Expand Down
37 changes: 28 additions & 9 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use dsc_lib::{
};
use std::process::exit;

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

match resource.get(input) {
Ok(result) => {
if let GetResult::Resource(response) = &result {
if format == Some(&GetOutputFormat::PassThrough) {
let json = match serde_json::to_string(&response.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;
}
}

// convert to json
let json = match serde_json::to_string(&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, false);
}
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_DSC_ERROR);
}
}
Expand All @@ -63,7 +82,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&GetOutputF
let export_result = match resource.export(&input) {
Ok(export) => { export }
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_DSC_ERROR);
}
};
Expand Down Expand Up @@ -127,14 +146,14 @@ pub fn set(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
let json = match serde_json::to_string(&result) {
Ok(json) => json,
Err(err) => {
error!("JSON Error: {err}");
error!("{}", t!("resource_command.jsonError", err = err));
exit(EXIT_JSON_ERROR);
}
};
write_object(&json, format, false);
}
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_DSC_ERROR);
}
}
Expand Down Expand Up @@ -191,7 +210,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, input: &str) {
match resource.delete(input) {
Ok(()) => {}
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_DSC_ERROR);
}
}
Expand All @@ -213,14 +232,14 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
match serde_json::from_str::<serde_json::Value>(json.as_str()) {
Ok(_) => (),
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_JSON_ERROR);
}
}
write_object(&json, format, false);
}
Err(err) => {
error!("Error: {err}");
error!("{err}");
exit(EXIT_DSC_ERROR);
}
}
Expand Down
20 changes: 8 additions & 12 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,20 +588,16 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
},
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
dsc.find_resources(&[resource.to_string()], progress_format);
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
if *all {
resource_command::get_all(&dsc, resource, output_format.as_ref());
}
else {
if *output_format == Some(GetOutputFormat::JsonArray) {
error!("{}", t!("subcommand.jsonArrayNotSupported"));
exit(EXIT_INVALID_ARGS);
}
let parsed_input = get_input(input.as_ref(), path.as_ref(), false);
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());
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
}
},
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
Expand Down
16 changes: 16 additions & 0 deletions dsc/tests/dsc_resource_get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ Describe 'resource get tests' {
$LASTEXITCODE | Should -Be 0
($out | Measure-Object).Count | Should -Be 1
}

It 'pass-through format works' {
$out = dsc resource get -r Microsoft/OSInfo --output-format pass-through | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$expectedFamily = if ($IsWindows) {
'Windows'
} elseif ($IsLinux) {
'Linux'
} else {
'macOS'
}
$out.family | Should -BeExactly $expectedFamily
$out.version | Should -Not -BeNullOrEmpty
$out.bitness | Should -BeIn @('32', '64')
$out.architecture | Should -BeIn @('x86', 'x86_64', 'arm64')
}
}
Loading