Skip to content

Commit

Permalink
Updated dsc args
Browse files Browse the repository at this point in the history
  • Loading branch information
anmenaga committed Jan 6, 2025
1 parent 9c2736d commit e35ae87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
26 changes: 13 additions & 13 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub enum SubCommand {
#[clap(name = "type", short, long, help = t!("args.schemaType").to_string(), value_enum)]
dsc_type: DscType,
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string(), value_enum)]
output_format: OutputFormat,
output_format: Option<OutputFormat>,
},
}

Expand All @@ -76,7 +76,7 @@ pub enum ConfigSubCommand {
#[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(), value_enum)]
output_format: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "set", about = t!("args.setAbout").to_string())]
Set {
Expand All @@ -85,7 +85,7 @@ pub enum ConfigSubCommand {
#[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: OutputFormat,
output_format: Option<OutputFormat>,
#[clap(short = 'w', long, help = t!("args.whatIf").to_string())]
what_if: bool,
},
Expand All @@ -96,7 +96,7 @@ pub enum ConfigSubCommand {
#[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: OutputFormat,
output_format: Option<OutputFormat>,
// Used by Assertion resource to return `test` result as a `get` result
#[clap(long, hide = true)]
as_get: bool,
Expand All @@ -111,7 +111,7 @@ pub enum ConfigSubCommand {
#[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: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "export", about = t!("args.exportAbout").to_string())]
Export {
Expand All @@ -120,7 +120,7 @@ pub enum ConfigSubCommand {
#[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: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "resolve", about = t!("args.resolveAbout").to_string(), hide = true)]
Resolve {
Expand All @@ -129,7 +129,7 @@ pub enum ConfigSubCommand {
#[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: OutputFormat,
output_format: Option<OutputFormat>,
}
}

Expand All @@ -147,7 +147,7 @@ pub enum ResourceSubCommand {
#[clap(short, long, help = t!("args.tags").to_string())]
tags: Option<Vec<String>>,
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
output_format: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "get", about = t!("args.resourceGet").to_string(), arg_required_else_help = true)]
Get {
Expand All @@ -160,7 +160,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: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
Set {
Expand All @@ -171,7 +171,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: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)]
Test {
Expand All @@ -182,7 +182,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: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "delete", about = "Invoke the delete operation to a resource", arg_required_else_help = true)]
Delete {
Expand All @@ -198,14 +198,14 @@ pub enum ResourceSubCommand {
#[clap(short, long, help = t!("args.resource").to_string())]
resource: String,
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
output_format: OutputFormat,
output_format: Option<OutputFormat>,
},
#[clap(name = "export", about = "Retrieve all resource instances", arg_required_else_help = true)]
Export {
#[clap(short, long, help = t!("args.resource").to_string())]
resource: String,
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
output_format: OutputFormat,
output_format: Option<OutputFormat>,
},
}

Expand Down
2 changes: 2 additions & 0 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rust_i18n::{i18n, t};
use std::{io, process::exit};
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
use tracing::{error, info, warn, debug};
use dsc_lib::util::OutputFormat;

#[cfg(debug_assertions)]
use crossterm::event;
Expand Down Expand Up @@ -73,6 +74,7 @@ fn main() {
exit(util::EXIT_JSON_ERROR);
}
};
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
util::write_output(&json, output_format);
},
}
Expand Down
53 changes: 33 additions & 20 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
}
};

configurator.set_output_format(*output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
configurator.set_output_format(output_format);

if let ConfigSubCommand::Set { what_if , .. } = subcommand {
if *what_if {
Expand Down Expand Up @@ -359,15 +360,19 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte

match subcommand {
ConfigSubCommand::Get { output_format, .. } => {
config_get(&mut configurator, *output_format, as_group);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
config_get(&mut configurator, output_format, as_group);
},
ConfigSubCommand::Set { output_format, .. } => {
config_set(&mut configurator, *output_format, as_group);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
config_set(&mut configurator, output_format, as_group);
},
ConfigSubCommand::Test { output_format, as_get, as_config, .. } => {
config_test(&mut configurator, *output_format, as_group, as_get, as_config);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
config_test(&mut configurator, output_format, as_group, as_get, as_config);
},
ConfigSubCommand::Validate { input, file, output_format} => {
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
let mut result = ValidateResult {
valid: true,
reason: None,
Expand All @@ -385,7 +390,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
}
}
} else {
match validate_config(configurator.get_config(), *output_format) {
match validate_config(configurator.get_config(), output_format) {
Ok(()) => {
// valid, so do nothing
},
Expand All @@ -401,12 +406,14 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
exit(EXIT_JSON_ERROR);
};

write_output(&json, *output_format);
write_output(&json, output_format);
},
ConfigSubCommand::Export { output_format, .. } => {
config_export(&mut configurator, *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
config_export(&mut configurator, output_format);
},
ConfigSubCommand::Resolve { output_format, .. } => {
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
let configuration = match serde_json::from_str(&json_string) {
Ok(json) => json,
Err(err) => {
Expand Down Expand Up @@ -435,7 +442,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
exit(EXIT_JSON_ERROR);
}
};
write_output(&json_string, *output_format);
write_output(&json_string, output_format);
},
}
}
Expand Down Expand Up @@ -541,33 +548,39 @@ pub fn resource(subcommand: &ResourceSubCommand) {

match subcommand {
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format } => {
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), output_format);
},
ResourceSubCommand::Schema { resource , output_format } => {
dsc.find_resources(&[resource.to_string()], *output_format);
resource_command::schema(&dsc, resource, *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
dsc.find_resources(&[resource.to_string()], output_format);
resource_command::schema(&dsc, resource, output_format);
},
ResourceSubCommand::Export { resource, output_format } => {
dsc.find_resources(&[resource.to_string()], *output_format);
resource_command::export(&mut dsc, resource, *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
dsc.find_resources(&[resource.to_string()], output_format);
resource_command::export(&mut dsc, resource, output_format);
},
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
dsc.find_resources(&[resource.to_string()], *output_format);
if *all { resource_command::get_all(&dsc, resource, *output_format); }
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
dsc.find_resources(&[resource.to_string()], output_format);
if *all { resource_command::get_all(&dsc, resource, output_format); }
else {
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::get(&dsc, resource, parsed_input, *output_format);
resource_command::get(&dsc, resource, parsed_input, output_format);
}
},
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
dsc.find_resources(&[resource.to_string()], *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
dsc.find_resources(&[resource.to_string()], output_format);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::set(&dsc, resource, parsed_input, *output_format);
resource_command::set(&dsc, resource, parsed_input, output_format);
},
ResourceSubCommand::Test { resource, input, file: path, output_format } => {
dsc.find_resources(&[resource.to_string()], *output_format);
let output_format = output_format.unwrap_or_else(|| { OutputFormat::None });
dsc.find_resources(&[resource.to_string()], output_format);
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::test(&dsc, resource, parsed_input, *output_format);
resource_command::test(&dsc, resource, parsed_input, output_format);
},
ResourceSubCommand::Delete { resource, input, file: path } => {
dsc.find_resources(&[resource.to_string()], OutputFormat::None); //TODO: add output_format to ResourceSubCommand::Delete
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use indicatif::ProgressStyle;

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
#[clap(hide = true)]
None,
Json,
PrettyJson,
Expand Down

0 comments on commit e35ae87

Please sign in to comment.