From e35ae8755222442d857ffb5ab72de158c3ebe84f Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 6 Jan 2025 13:48:37 -0800 Subject: [PATCH] Updated dsc args --- dsc/src/args.rs | 26 ++++++++++----------- dsc/src/main.rs | 2 ++ dsc/src/subcommand.rs | 53 +++++++++++++++++++++++++++---------------- dsc_lib/src/util.rs | 1 + 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 89333fc6..6d4a6ab8 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -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, }, } @@ -76,7 +76,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string(), value_enum)] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "set", about = t!("args.setAbout").to_string())] Set { @@ -85,7 +85,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, #[clap(short = 'w', long, help = t!("args.whatIf").to_string())] what_if: bool, }, @@ -96,7 +96,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, // Used by Assertion resource to return `test` result as a `get` result #[clap(long, hide = true)] as_get: bool, @@ -111,7 +111,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "export", about = t!("args.exportAbout").to_string())] Export { @@ -120,7 +120,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "resolve", about = t!("args.resolveAbout").to_string(), hide = true)] Resolve { @@ -129,7 +129,7 @@ pub enum ConfigSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, } } @@ -147,7 +147,7 @@ pub enum ResourceSubCommand { #[clap(short, long, help = t!("args.tags").to_string())] tags: Option>, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "get", about = t!("args.resourceGet").to_string(), arg_required_else_help = true)] Get { @@ -160,7 +160,7 @@ pub enum ResourceSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)] Set { @@ -171,7 +171,7 @@ pub enum ResourceSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)] Test { @@ -182,7 +182,7 @@ pub enum ResourceSubCommand { #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] - output_format: OutputFormat, + output_format: Option, }, #[clap(name = "delete", about = "Invoke the delete operation to a resource", arg_required_else_help = true)] Delete { @@ -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, }, #[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, }, } diff --git a/dsc/src/main.rs b/dsc/src/main.rs index ace139c0..f466aa9a 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -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; @@ -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); }, } diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index 7b54dd78..9bc80673 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -296,7 +296,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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 { @@ -359,15 +360,19 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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, @@ -385,7 +390,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, mounte } } } else { - match validate_config(configurator.get_config(), *output_format) { + match validate_config(configurator.get_config(), output_format) { Ok(()) => { // valid, so do nothing }, @@ -401,12 +406,14 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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) => { @@ -435,7 +442,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, mounte exit(EXIT_JSON_ERROR); } }; - write_output(&json_string, *output_format); + write_output(&json_string, output_format); }, } } @@ -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 diff --git a/dsc_lib/src/util.rs b/dsc_lib/src/util.rs index 00f5f204..a3a5b9b0 100644 --- a/dsc_lib/src/util.rs +++ b/dsc_lib/src/util.rs @@ -18,6 +18,7 @@ use indicatif::ProgressStyle; #[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] pub enum OutputFormat { + #[clap(hide = true)] None, Json, PrettyJson,