Skip to content
38 changes: 31 additions & 7 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use crate::util::LogLevel;

/// The output format to use
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
Json,
Expand All @@ -18,9 +19,6 @@ pub struct Args {
/// The subcommand to run
#[clap(subcommand)]
pub subcommand: SubCommand,
/// The output format to use
#[clap(short = 'f', long)]
pub format: Option<OutputFormat>,
#[clap(short = 'i', long, help = "The input to pass to the configuration or resource", conflicts_with = "input_file")]
pub input: Option<String>,
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource")]
Expand Down Expand Up @@ -54,21 +52,35 @@ pub enum SubCommand {
Schema {
#[clap(name = "type", short, long, help = "The type of DSC schema to get")]
dsc_type: DscType,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
pub enum ConfigSubCommand {
#[clap(name = "get", about = "Retrieve the current configuration")]
Get,
Get {
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "set", about = "Set the current configuration")]
Set,
Set {
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "test", about = "Test the current configuration")]
Test,
Test {
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "validate", about = "Validate the current configuration", hide = true)]
Validate,
#[clap(name = "export", about = "Export the current configuration")]
Export
Export {
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
}
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
Expand All @@ -81,6 +93,8 @@ pub enum ResourceSubCommand {
description: Option<String>,
#[clap(short, long, help = "Tag to search for in the resource tags")]
tags: Option<Vec<String>>,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "get", about = "Invoke the get operation to a resource", arg_required_else_help = true)]
Get {
Expand All @@ -90,30 +104,40 @@ pub enum ResourceSubCommand {
resource: String,
#[clap(short, long, help = "The input to pass to the resource as JSON")]
input: Option<String>,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
Set {
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `set` on")]
resource: String,
#[clap(short, long, help = "The input to pass to the resource as JSON")]
input: Option<String>,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)]
Test {
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `test` on")]
resource: String,
#[clap(short, long, help = "The input to pass to the resource as JSON")]
input: Option<String>,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "schema", about = "Get the JSON schema for a resource", arg_required_else_help = true)]
Schema {
#[clap(short, long, help = "The name of the resource to get the JSON schema")]
resource: String,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
#[clap(name = "export", about = "Retrieve all resource instances", arg_required_else_help = true)]
Export {
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `export` on")]
resource: String,
#[clap(short = 'f', long)]
format: Option<OutputFormat>,
},
}

Expand Down
10 changes: 5 additions & 5 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ fn main() {
if let Some(file_name) = parameters_file {
info!("Reading parameters from file {}", file_name);
match std::fs::read_to_string(file_name) {
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &args.format, &input),
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &input),
Err(err) => {
error!("Error: Failed to read parameters file: {err}");
exit(util::EXIT_INVALID_INPUT);
}
}
}
else {
subcommand::config(&subcommand, &parameters, &args.format, &input);
subcommand::config(&subcommand, &parameters, &input);
}
},
SubCommand::Resource { subcommand } => {
subcommand::resource(&subcommand, &args.format, &input);
subcommand::resource(&subcommand, &input);
},
SubCommand::Schema { dsc_type } => {
SubCommand::Schema { dsc_type , format } => {
let schema = util::get_schema(dsc_type);
let json = match serde_json::to_string(&schema) {
Ok(json) => json,
Expand All @@ -108,7 +108,7 @@ fn main() {
exit(util::EXIT_JSON_ERROR);
}
};
util::write_output(&json, &args.format);
util::write_output(&json, &format);
},
}

Expand Down
24 changes: 12 additions & 12 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn config_export(configurator: &mut Configurator, format: &Option<OutputForm
}
}

pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, format: &Option<OutputFormat>, stdin: &Option<String>) {
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, stdin: &Option<String>) {
let Some(stdin) = stdin else {
error!("Configuration must be piped to STDIN");
exit(EXIT_INVALID_ARGS);
Expand Down Expand Up @@ -184,19 +184,19 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, format
}

match subcommand {
ConfigSubCommand::Get => {
ConfigSubCommand::Get { format } => {
config_get(&mut configurator, format);
},
ConfigSubCommand::Set => {
ConfigSubCommand::Set { format } => {
config_set(&mut configurator, format);
},
ConfigSubCommand::Test => {
ConfigSubCommand::Test { format } => {
config_test(&mut configurator, format);
},
ConfigSubCommand::Validate => {
validate_config(&json_string);
},
ConfigSubCommand::Export => {
ConfigSubCommand::Export { format } => {
config_export(&mut configurator, format);
}
}
Expand Down Expand Up @@ -324,7 +324,7 @@ pub fn validate_config(config: &str) {
exit(EXIT_SUCCESS);
}

pub fn resource(subcommand: &ResourceSubCommand, format: &Option<OutputFormat>, stdin: &Option<String>) {
pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {
let mut dsc = match DscManager::new() {
Ok(dsc) => dsc,
Err(err) => {
Expand All @@ -334,7 +334,7 @@ pub fn resource(subcommand: &ResourceSubCommand, format: &Option<OutputFormat>,
};

match subcommand {
ResourceSubCommand::List { resource_name, description, tags } => {
ResourceSubCommand::List { resource_name, description, tags, format } => {

let mut write_table = false;
let mut table = Table::new(&["Type", "Version", "Requires", "Description"]);
Expand Down Expand Up @@ -411,26 +411,26 @@ pub fn resource(subcommand: &ResourceSubCommand, format: &Option<OutputFormat>,
table.print();
}
},
ResourceSubCommand::Get { resource, input, all } => {
ResourceSubCommand::Get { resource, input, all, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
if *all { resource_command::get_all(&dsc, resource, input, stdin, format); }
else {
resource_command::get(&dsc, resource, input, stdin, format);
};
},
ResourceSubCommand::Set { resource, input } => {
ResourceSubCommand::Set { resource, input, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
resource_command::set(&dsc, resource, input, stdin, format);
},
ResourceSubCommand::Test { resource, input } => {
ResourceSubCommand::Test { resource, input, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
resource_command::test(&dsc, resource, input, stdin, format);
},
ResourceSubCommand::Schema { resource } => {
ResourceSubCommand::Schema { resource , format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
resource_command::schema(&dsc, resource, format);
},
ResourceSubCommand::Export { resource} => {
ResourceSubCommand::Export { resource, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
resource_command::export(&mut dsc, resource, format);
},
Expand Down
2 changes: 1 addition & 1 deletion dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ actualState:
) {
param($format, $expected)

$out = dsc --format $format resource get -r Test/Hello | Out-String
$out = dsc resource get -r Test/Hello --format $format | Out-String
$LASTEXITCODE | Should -Be 0
$out.Trim() | Should -BeExactly $expected
}
Expand Down