diff --git a/lib/cli/src/commands/app/deploy.rs b/lib/cli/src/commands/app/deploy.rs index 25c3dd6a2f7..532cd545dd9 100644 --- a/lib/cli/src/commands/app/deploy.rs +++ b/lib/cli/src/commands/app/deploy.rs @@ -587,7 +587,7 @@ impl AsyncCliCommand for CmdAppDeploy { wait_app(&client, opts.clone(), app_version.clone(), self.quiet).await?; - if self.fmt.format == crate::utils::render::ItemFormat::Json { + if self.fmt.format == Some(crate::utils::render::ItemFormat::Json) { println!("{}", serde_json::to_string_pretty(&app_version)?); } diff --git a/lib/cli/src/commands/app/deployments/get.rs b/lib/cli/src/commands/app/deployments/get.rs index 90bdd5ee0f3..07ccb4150ea 100644 --- a/lib/cli/src/commands/app/deployments/get.rs +++ b/lib/cli/src/commands/app/deployments/get.rs @@ -1,8 +1,6 @@ //! Get deployments for an app. -use crate::{ - commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts, utils::render::ItemFormat, -}; +use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts}; /// Get the volumes of an app. #[derive(clap::Parser, Debug)] @@ -25,13 +23,7 @@ impl AsyncCliCommand for CmdAppDeploymentGet { let client = self.env.client()?; let item = wasmer_api::query::app_deployment(&client, self.id).await?; - // The below is a bit hacky - the default is YAML, but this is not a good - // default for deployments, so we switch to table. - if self.fmt.format == ItemFormat::Yaml { - self.fmt.format = ItemFormat::Table; - } - - println!("{}", self.fmt.format.render(&item)); + println!("{}", self.fmt.get().render(&item)); Ok(()) } } diff --git a/lib/cli/src/commands/app/get.rs b/lib/cli/src/commands/app/get.rs index 73b4d9de94f..12143139a31 100644 --- a/lib/cli/src/commands/app/get.rs +++ b/lib/cli/src/commands/app/get.rs @@ -4,7 +4,9 @@ use wasmer_api::types::DeployApp; use super::util::AppIdentOpts; -use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts}; +use crate::{ + commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts, utils::render::ItemFormat, +}; /// Retrieve detailed informations about an app #[derive(clap::Parser, Debug)] @@ -27,7 +29,10 @@ impl AsyncCliCommand for CmdAppGet { let client = self.env.client()?; let (_ident, app) = self.ident.load_app(&client).await?; - println!("{}", self.fmt.format.render(&app)); + println!( + "{}", + self.fmt.get_with_default(ItemFormat::Yaml).render(&app) + ); Ok(app) } diff --git a/lib/cli/src/commands/app/version/get.rs b/lib/cli/src/commands/app/version/get.rs index 83d80edffa2..aa199644c10 100644 --- a/lib/cli/src/commands/app/version/get.rs +++ b/lib/cli/src/commands/app/version/get.rs @@ -4,6 +4,7 @@ use crate::{ commands::{app::util::AppIdentOpts, AsyncCliCommand}, config::WasmerEnv, opts::ItemFormatOpts, + utils::render::ItemFormat, }; /// Show information for a specific app version. @@ -41,7 +42,10 @@ impl AsyncCliCommand for CmdAppVersionGet { .await? .with_context(|| format!("Could not find app version '{}'", self.name))?; - println!("{}", self.fmt.format.render(&version)); + println!( + "{}", + self.fmt.get_with_default(ItemFormat::Yaml).render(&version) + ); Ok(()) } diff --git a/lib/cli/src/commands/namespace/create.rs b/lib/cli/src/commands/namespace/create.rs index adc6b3017f4..4770eaaf381 100644 --- a/lib/cli/src/commands/namespace/create.rs +++ b/lib/cli/src/commands/namespace/create.rs @@ -30,7 +30,7 @@ impl AsyncCliCommand for CmdNamespaceCreate { }; let namespace = wasmer_api::query::create_namespace(&client, vars).await?; - println!("{}", self.fmt.format.render(&namespace)); + println!("{}", self.fmt.get().render(&namespace)); Ok(()) } diff --git a/lib/cli/src/commands/namespace/get.rs b/lib/cli/src/commands/namespace/get.rs index 48138de8309..9ca3a0cfcc7 100644 --- a/lib/cli/src/commands/namespace/get.rs +++ b/lib/cli/src/commands/namespace/get.rs @@ -26,7 +26,7 @@ impl AsyncCliCommand for CmdNamespaceGet { .await? .context("namespace not found")?; - println!("{}", self.fmt.format.render(&namespace)); + println!("{}", self.fmt.get().render(&namespace)); Ok(()) } diff --git a/lib/cli/src/opts.rs b/lib/cli/src/opts.rs index cc9844a2197..8b0a983c085 100644 --- a/lib/cli/src/opts.rs +++ b/lib/cli/src/opts.rs @@ -1,9 +1,30 @@ +use crate::utils::render::ItemFormat; + /// Formatting options for a single item. #[derive(clap::Parser, Debug, Default)] pub struct ItemFormatOpts { /// Output format. (yaml, json, table) - #[clap(short = 'f', long, default_value = "yaml")] - pub format: crate::utils::render::ItemFormat, + /// + /// This value is optional instead of using a default value to allow code + /// to distinguish between the user not specifying a value and a generic + /// default. + /// + /// Code should usually use [`Self::get`] to use the same default format. + #[clap(short = 'f', long)] + pub format: Option, +} + +impl ItemFormatOpts { + /// Get the output format, defaulting to `ItemFormat::Yaml`. + pub fn get(&self) -> crate::utils::render::ItemFormat { + self.format + .unwrap_or(crate::utils::render::ItemFormat::Table) + } + + /// Get the output format, defaulting to the given value if not specified. + pub fn get_with_default(&self, default: ItemFormat) -> crate::utils::render::ItemFormat { + self.format.unwrap_or(default) + } } /// Formatting options for a single item.