diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 6d4a6ab8..89333fc6 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: Option, + output_format: OutputFormat, }, } @@ -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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, #[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: Option, + output_format: OutputFormat, // 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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, } } @@ -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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, }, #[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: Option, + output_format: OutputFormat, }, #[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: Option, + 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, + output_format: OutputFormat, }, } diff --git a/dsc/src/main.rs b/dsc/src/main.rs index f3c0d1f2..ace139c0 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -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); }, } diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index dd1237f0..44772e36 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -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); @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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()); diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index b33a41ec..7b54dd78 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -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) => { @@ -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) => { @@ -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) => { @@ -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) => { @@ -288,7 +288,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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}"); @@ -296,6 +296,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, mounte } }; + configurator.set_output_format(*output_format); + if let ConfigSubCommand::Set { what_if , .. } = subcommand { if *what_if { configurator.context.execution_type = ExecutionKind::WhatIf; @@ -357,13 +359,13 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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 { @@ -383,7 +385,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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 }, @@ -399,10 +401,10 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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) { @@ -433,7 +435,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, mounte exit(EXIT_JSON_ERROR); } }; - write_output(&json_string, output_format.as_ref()); + write_output(&json_string, *output_format); }, } } @@ -451,7 +453,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, 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))?; @@ -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>, format: Option<&OutputFormat>) { +fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec>, format: OutputFormat) { let mut write_table = false; let mut table = Table::new(&[ t!("subcommand.tableHeader_type").to_string().as_ref(), @@ -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; } diff --git a/dsc/src/util.rs b/dsc/src/util.rs index 4fa42ccd..0d313f15 100644 --- a/dsc/src/util.rs +++ b/dsc/src/util.rs @@ -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) => { @@ -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, diff --git a/dsc_lib/src/configure/mod.rs b/dsc_lib/src/configure/mod.rs index 70e590a1..271a34c7 100644 --- a/dsc_lib/src/configure/mod.rs +++ b/dsc_lib/src/configure/mod.rs @@ -13,7 +13,7 @@ use crate::DscResource; use crate::discovery::Discovery; use crate::parser::Statement; use crate::OutputFormat; -use crate::util::DscProgressBar; +use crate::util::ProgressBar; use self::context::Context; use self::config_doc::{Configuration, DataType, MicrosoftDscMetadata, Operation, SecurityContextKind}; use self::depends_on::get_resource_invocation_order; @@ -32,13 +32,13 @@ pub mod contraints; pub mod depends_on; pub mod parameters; -pub struct Configurator<'a> { +pub struct Configurator { json: String, config: Configuration, pub context: Context, discovery: Discovery, statement_parser: Statement, - progress_format: Option<&'a OutputFormat>, + progress_format: OutputFormat, } /// Add the results of an export operation to a configuration. @@ -136,8 +136,8 @@ fn escape_property_values(properties: &Map) -> Result) -> Result { - let mut pb_span = DscProgressBar::new(progress_format.is_some_and(|v| (v == &OutputFormat::Json) || (v == &OutputFormat::PrettyJson))); +fn get_progress_bar_span(len: u64, progress_format: OutputFormat) -> Result { + let mut pb_span = ProgressBar::new((progress_format == OutputFormat::Json) || (progress_format == OutputFormat::PrettyJson)); pb_span.pb_set_style(&ProgressStyle::with_template( "{spinner:.green} [{elapsed_precise:.cyan}] [{bar:40.cyan/blue}] {pos:>7}/{len:7} {msg:.yellow}" @@ -194,7 +194,7 @@ fn check_security_context(metadata: Option<&Metadata>) -> Result<(), DscError> { Ok(()) } -impl Configurator<'_> { +impl Configurator { /// Create a new `Configurator` instance. /// /// # Arguments @@ -204,7 +204,7 @@ impl Configurator<'_> { /// # Errors /// /// This function will return an error if the configuration is invalid or the underlying discovery fails. - pub fn new<'a>(json: &'a str, progress_format: Option<&'a OutputFormat>) -> Result, DscError> { + pub fn new(json: &str) -> Result { let discovery = Discovery::new()?; let mut config = Configurator { json: json.to_owned(), @@ -212,7 +212,7 @@ impl Configurator<'_> { context: Context::new(), discovery, statement_parser: Statement::new()?, - progress_format, + progress_format: OutputFormat::None, }; config.validate_config()?; Ok(config) @@ -228,6 +228,11 @@ impl Configurator<'_> { &self.config } + /// Sets progress format for the configuration. + pub fn set_output_format(&mut self, progress_format: OutputFormat) { + self.progress_format = progress_format; + } + /// Invoke the get operation on a resource. /// /// # Returns diff --git a/dsc_lib/src/discovery/command_discovery.rs b/dsc_lib/src/discovery/command_discovery.rs index 707c8151..008b510d 100644 --- a/dsc_lib/src/discovery/command_discovery.rs +++ b/dsc_lib/src/discovery/command_discovery.rs @@ -24,7 +24,7 @@ use std::str::FromStr; use tracing::{debug, info, trace, warn, warn_span}; use tracing_indicatif::span_ext::IndicatifSpanExt; -use crate::util::{get_setting, DscProgressBar}; +use crate::util::{get_setting, ProgressBar}; pub struct CommandDiscovery { // use BTreeMap so that the results are sorted by the typename, the Vec is sorted by version @@ -245,7 +245,7 @@ impl ResourceDiscovery for CommandDiscovery { Ok(()) } - fn discover_adapted_resources(&mut self, name_filter: &str, adapter_filter: &str, progress_format: Option<&OutputFormat>) -> Result<(), DscError> { + fn discover_adapted_resources(&mut self, name_filter: &str, adapter_filter: &str, progress_format: OutputFormat) -> Result<(), DscError> { if self.resources.is_empty() && self.adapters.is_empty() { self.discover_resources("*")?; } @@ -270,7 +270,7 @@ impl ResourceDiscovery for CommandDiscovery { return Err(DscError::Operation("Could not build Regex filter for resource name".to_string())); }; - let mut pb_span = DscProgressBar::new(progress_format.is_some_and(|v| (v == &OutputFormat::Json) || (v == &OutputFormat::PrettyJson))); + let mut pb_span = ProgressBar::new((progress_format == OutputFormat::Json) || (progress_format == OutputFormat::PrettyJson)); pb_span.pb_set_style(&ProgressStyle::with_template( "{spinner:.green} [{elapsed_precise:.cyan}] [{bar:40.cyan/blue}] {pos:>7}/{len:7} {msg:.yellow}" )?); @@ -291,7 +291,7 @@ impl ResourceDiscovery for CommandDiscovery { found_adapter = true; info!("Enumerating resources for adapter '{}'", adapter_name); - let mut pb_adapter_span = DscProgressBar::new(progress_format.is_some_and(|v| (v == &OutputFormat::Json) || (v == &OutputFormat::PrettyJson))); + let mut pb_adapter_span = ProgressBar::new((progress_format == OutputFormat::Json) || (progress_format == OutputFormat::PrettyJson)); pb_adapter_span.pb_set_style(&ProgressStyle::with_template( "{spinner:.green} [{elapsed_precise:.cyan}] {msg:.white}" )?); @@ -361,7 +361,7 @@ impl ResourceDiscovery for CommandDiscovery { Ok(()) } - fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: Option<&OutputFormat>) -> Result>, DscError> { + fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: OutputFormat) -> Result>, DscError> { trace!("Listing resources with type_name_filter '{type_name_filter}' and adapter_name_filter '{adapter_name_filter}'"); let mut resources = BTreeMap::>::new(); @@ -385,7 +385,7 @@ impl ResourceDiscovery for CommandDiscovery { } // TODO: handle version requirements - fn find_resources(&mut self, required_resource_types: &[String], progress_format: Option<&OutputFormat>) -> Result, DscError> + fn find_resources(&mut self, required_resource_types: &[String], progress_format: OutputFormat) -> Result, DscError> { debug!("Searching for resources: {:?}", required_resource_types); self.discover_resources("*")?; diff --git a/dsc_lib/src/discovery/discovery_trait.rs b/dsc_lib/src/discovery/discovery_trait.rs index f19f9f7d..ccc09f17 100644 --- a/dsc_lib/src/discovery/discovery_trait.rs +++ b/dsc_lib/src/discovery/discovery_trait.rs @@ -7,7 +7,7 @@ use crate::util::OutputFormat; pub trait ResourceDiscovery { fn discover_resources(&mut self, filter: &str) -> Result<(), DscError>; - fn discover_adapted_resources(&mut self, name_filter: &str, adapter_filter: &str, progress_format: Option<&OutputFormat>) -> Result<(), DscError>; - fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: Option<&OutputFormat>) -> Result>, DscError>; - fn find_resources(&mut self, required_resource_types: &[String], progress_format: Option<&OutputFormat>) -> Result, DscError>; + fn discover_adapted_resources(&mut self, name_filter: &str, adapter_filter: &str, progress_format: OutputFormat) -> Result<(), DscError>; + fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: OutputFormat) -> Result>, DscError>; + fn find_resources(&mut self, required_resource_types: &[String], progress_format: OutputFormat) -> Result, DscError>; } diff --git a/dsc_lib/src/discovery/mod.rs b/dsc_lib/src/discovery/mod.rs index cafb04b4..667da83d 100644 --- a/dsc_lib/src/discovery/mod.rs +++ b/dsc_lib/src/discovery/mod.rs @@ -37,7 +37,7 @@ impl Discovery { /// # Returns /// /// A vector of `DscResource` instances. - pub fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: Option<&OutputFormat>) -> Vec { + pub fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: OutputFormat) -> Vec { let discovery_types: Vec> = vec![ Box::new(command_discovery::CommandDiscovery::new()), ]; @@ -74,7 +74,7 @@ impl Discovery { /// # Arguments /// /// * `required_resource_types` - The required resource types. - pub fn find_resources(&mut self, required_resource_types: &[String], progress_format: Option<&OutputFormat>) { + pub fn find_resources(&mut self, required_resource_types: &[String], progress_format: OutputFormat) { let discovery_types: Vec> = vec![ Box::new(command_discovery::CommandDiscovery::new()), ]; diff --git a/dsc_lib/src/lib.rs b/dsc_lib/src/lib.rs index 828ab9fd..e58d011a 100644 --- a/dsc_lib/src/lib.rs +++ b/dsc_lib/src/lib.rs @@ -42,11 +42,11 @@ impl DscManager { self.discovery.find_resource(name) } - pub fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: Option<&OutputFormat>) -> Vec { + pub fn list_available_resources(&mut self, type_name_filter: &str, adapter_name_filter: &str, progress_format: OutputFormat) -> Vec { self.discovery.list_available_resources(type_name_filter, adapter_name_filter, progress_format) } - pub fn find_resources(&mut self, required_resource_types: &[String], progress_format: Option<&OutputFormat>) { + pub fn find_resources(&mut self, required_resource_types: &[String], progress_format: OutputFormat) { self.discovery.find_resources(required_resource_types, progress_format); } /// Invoke the get operation on a resource. diff --git a/dsc_lib/src/util.rs b/dsc_lib/src/util.rs index cb1a90e7..00f5f204 100644 --- a/dsc_lib/src/util.rs +++ b/dsc_lib/src/util.rs @@ -16,8 +16,9 @@ use tracing::warn_span; use tracing::span::Span; use indicatif::ProgressStyle; -#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] pub enum OutputFormat { + None, Json, PrettyJson, Yaml, @@ -38,25 +39,25 @@ impl Default for DscSettingValue { } #[derive(Default, Debug, Clone, Serialize)] -pub struct DscProgressValue { +pub struct Progress { pub activity: String, pub status: String, pub percent_complete: u16, pub seconds_remaining: u64, } -pub struct DscProgressBar { - progress_value: DscProgressValue, +pub struct ProgressBar { + progress_value: Progress, ui_bar: Span, //IndicatifSpanExt length: u64, position: u64, emit_json: bool } -impl DscProgressBar { - pub fn new(emit_json: bool) -> DscProgressBar { - DscProgressBar { - progress_value: DscProgressValue::default(), +impl ProgressBar { + pub fn new(emit_json: bool) -> ProgressBar { + ProgressBar { + progress_value: Progress::default(), ui_bar: warn_span!(""), length: 0, position: 0,