Skip to content

Commit

Permalink
Added OutputFormat::None
Browse files Browse the repository at this point in the history
  • Loading branch information
anmenaga committed Jan 6, 2025
1 parent b5a6b73 commit 9c2736d
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 85 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: 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: Option<OutputFormat>,
output_format: OutputFormat,
},
}

Expand Down
2 changes: 1 addition & 1 deletion dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main() {
exit(util::EXIT_JSON_ERROR);
}
};
util::write_output(&json, output_format.as_ref());
util::write_output(&json, output_format);
},
}

Expand Down
12 changes: 6 additions & 6 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, mut input: String, format: Option<&OutputFormat>) {
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: OutputFormat) {
let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
}
}

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

pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: OutputFormat) {
if input.is_empty() {
error!("{}", t!("resource_command.setInputEmpty"));
exit(EXIT_INVALID_ARGS);
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
}
}

pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: OutputFormat) {
if input.is_empty() {
error!("{}", t!("resource_command.testInputEmpty"));
exit(EXIT_INVALID_ARGS);
Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
}
}

pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
pub fn schema(dsc: &DscManager, resource_type: &str, format: OutputFormat) {
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 Down Expand Up @@ -257,7 +257,7 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
}
}

pub fn export(dsc: &mut DscManager, resource_type: &str, format: Option<&OutputFormat>) {
pub fn export(dsc: &mut DscManager, resource_type: &str, format: OutputFormat) {
let mut input = String::new();
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
Expand Down
58 changes: 30 additions & 28 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::{
};
use tracing::{debug, error, trace};

pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
pub fn config_get(configurator: &mut Configurator, format: OutputFormat, as_group: &bool)
{
match configurator.invoke_get() {
Ok(result) => {
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn config_get(configurator: &mut Configurator, format: Option<&OutputFormat>
}
}

pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool)
pub fn config_set(configurator: &mut Configurator, format: OutputFormat, as_group: &bool)
{
match configurator.invoke_set(false) {
Ok(result) => {
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn config_set(configurator: &mut Configurator, format: Option<&OutputFormat>
}
}

pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
pub fn config_test(configurator: &mut Configurator, format: OutputFormat, as_group: &bool, as_get: &bool, as_config: &bool)
{
match configurator.invoke_test() {
Ok(result) => {
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat
}
}

pub fn config_export(configurator: &mut Configurator, format: Option<&OutputFormat>)
pub fn config_export(configurator: &mut Configurator, format: OutputFormat)
{
match configurator.invoke_export() {
Ok(result) => {
Expand Down Expand Up @@ -288,14 +288,16 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
}
};

let mut configurator = match Configurator::new(&json_string, output_format.as_ref()) {
let mut configurator = match Configurator::new(&json_string) {
Ok(configurator) => configurator,
Err(err) => {
error!("Error: {err}");
exit(EXIT_DSC_ERROR);
}
};

configurator.set_output_format(*output_format);

if let ConfigSubCommand::Set { what_if , .. } = subcommand {
if *what_if {
configurator.context.execution_type = ExecutionKind::WhatIf;
Expand Down Expand Up @@ -357,13 +359,13 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte

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

write_output(&json, output_format.as_ref());
write_output(&json, *output_format);
},
ConfigSubCommand::Export { output_format, .. } => {
config_export(&mut configurator, output_format.as_ref());
config_export(&mut configurator, *output_format);
},
ConfigSubCommand::Resolve { output_format, .. } => {
let configuration = match serde_json::from_str(&json_string) {
Expand Down Expand Up @@ -433,7 +435,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
exit(EXIT_JSON_ERROR);
}
};
write_output(&json_string, output_format.as_ref());
write_output(&json_string, *output_format);
},
}
}
Expand All @@ -451,7 +453,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
/// # Errors
///
/// * `DscError` - The error that occurred.
pub fn validate_config(config: &Configuration, progress_format: Option<&OutputFormat>) -> Result<(), DscError> {
pub fn validate_config(config: &Configuration, progress_format: OutputFormat) -> Result<(), DscError> {
// first validate against the config schema
debug!("{}", t!("subcommand.validatingConfiguration"));
let schema = serde_json::to_value(get_schema(DscType::Configuration))?;
Expand Down Expand Up @@ -539,43 +541,43 @@ 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.as_ref());
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.as_ref());
resource_command::schema(&dsc, resource, output_format.as_ref());
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.as_ref());
resource_command::export(&mut dsc, resource, output_format.as_ref());
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.as_ref());
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
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.as_ref());
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.as_ref());
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.as_ref());
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.as_ref());
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.as_ref());
resource_command::test(&dsc, resource, parsed_input, *output_format);
},
ResourceSubCommand::Delete { resource, input, file: path } => {
dsc.find_resources(&[resource.to_string()], None); //TODO: add output_format to ResourceSubCommand::Delete
dsc.find_resources(&[resource.to_string()], OutputFormat::None); //TODO: add output_format to ResourceSubCommand::Delete
let parsed_input = get_input(input.as_ref(), path.as_ref());
resource_command::delete(&dsc, resource, parsed_input);
},
}
}

fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&OutputFormat>) {
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: OutputFormat) {
let mut write_table = false;
let mut table = Table::new(&[
t!("subcommand.tableHeader_type").to_string().as_ref(),
Expand All @@ -585,7 +587,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
t!("subcommand.tableheader_adapter").to_string().as_ref(),
t!("subcommand.tableheader_description").to_string().as_ref(),
]);
if format.is_none() && io::stdout().is_terminal() {
if format == OutputFormat::None && io::stdout().is_terminal() {
// write as table if format is not specified and interactive
write_table = true;
}
Expand Down
16 changes: 8 additions & 8 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,23 @@ pub fn get_schema(dsc_type: DscType) -> RootSchema {
///
/// * `json` - The JSON to write
/// * `format` - The format to use
pub fn write_output(json: &str, format: Option<&OutputFormat>) {
pub fn write_output(json: &str, format: OutputFormat) {
let mut is_json = true;
let mut output_format = format;
let mut syntax_color = false;
if std::io::stdout().is_terminal() {
syntax_color = true;
if output_format.is_none() {
output_format = Some(&OutputFormat::Yaml);
if output_format == OutputFormat::None {
output_format = OutputFormat::Yaml;
}
}
else if output_format.is_none() {
output_format = Some(&OutputFormat::Json);
else if output_format == OutputFormat::None {
output_format = OutputFormat::Json;
}

let output = match output_format {
Some(OutputFormat::Json) => json.to_string(),
Some(OutputFormat::PrettyJson) => {
OutputFormat::Json => json.to_string(),
OutputFormat::PrettyJson => {
let value: serde_json::Value = match serde_json::from_str(json) {
Ok(value) => value,
Err(err) => {
Expand All @@ -247,7 +247,7 @@ pub fn write_output(json: &str, format: Option<&OutputFormat>) {
}
}
},
Some(OutputFormat::Yaml) | None => {
OutputFormat::Yaml | OutputFormat::None => {
is_json = false;
let value: serde_json::Value = match serde_json::from_str(json) {
Ok(value) => value,
Expand Down
Loading

0 comments on commit 9c2736d

Please sign in to comment.