Skip to content

Commit

Permalink
chore: Change default display format for individual items to table
Browse files Browse the repository at this point in the history
Make ItemFormatOpts::fmt to be optional so code can distinguish between
not-provided and default value.

Make the default access method to be `ItemFormatOpts::get()`, which uses
the table format as a default.

Also adds a ItemFormatOpts::get_with_default() for cases where a custom
default is desired.
  • Loading branch information
theduke committed Oct 22, 2024
1 parent 22a6f70 commit dd09e78
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/cli/src/commands/app/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?);
}

Expand Down
12 changes: 2 additions & 10 deletions lib/cli/src/commands/app/deployments/get.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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(())
}
}
9 changes: 7 additions & 2 deletions lib/cli/src/commands/app/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/src/commands/app/version/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/namespace/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
25 changes: 23 additions & 2 deletions lib/cli/src/opts.rs
Original file line number Diff line number Diff line change
@@ -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<ItemFormat>,
}

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.
Expand Down

0 comments on commit dd09e78

Please sign in to comment.