Skip to content

Commit 43f28b0

Browse files
committed
feat(boil): Add human-readable output (default)
1 parent 1475a8e commit 43f28b0

5 files changed

Lines changed: 61 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clap_complete = "4.5.55"
1414
clap_complete_nushell = "4.5.8"
1515
git2 = "0.20.1"
1616
glob = "0.3.2"
17+
humansize = "2.1.3"
1718
oci-spec = "0.9.0"
1819
reqwest = { version = "0.13.2", features = ["json"] }
1920
rstest = "0.26.1"

rust/boil/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clap_complete.workspace = true
1414
clap_complete_nushell.workspace = true
1515
git2.workspace = true
1616
glob.workspace = true
17+
humansize.workspace = true
1718
oci-spec.workspace = true
1819
reqwest.workspace = true
1920
regex.workspace = true

rust/boil/src/cli/image.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ pub struct ImageSizeArguments {
7777
help_heading = "Image Options"
7878
)]
7979
pub target_platform: TargetPlatform,
80+
81+
/// Pretty print the structured output.
82+
#[arg(long, value_enum, default_value_t = Pretty::default(), help_heading = "Output Options")]
83+
pub pretty: Pretty,
84+
85+
#[arg(short, long, value_enum, default_value_t = Format::default(), help_heading = "Output Options")]
86+
pub format: Format,
8087
}
8188

8289
impl ImageSizeArguments {
@@ -86,11 +93,17 @@ impl ImageSizeArguments {
8693
}
8794
}
8895

89-
// #[derive(Clone, Debug, Default, strum::Display, strum::EnumString)]
9096
#[derive(Clone, Debug, Default, ValueEnum)]
9197
pub enum Pretty {
9298
#[default]
9399
Auto,
94100
Always,
95101
Never,
96102
}
103+
104+
#[derive(Clone, Debug, Default, ValueEnum)]
105+
pub enum Format {
106+
#[default]
107+
Plain,
108+
Json,
109+
}

rust/boil/src/cmd/image.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::Serialize;
88
use snafu::{ResultExt, Snafu};
99

1010
use crate::{
11-
cli::{ImageCheckArguments, ImageListArguments, ImageSizeArguments, Pretty},
11+
cli::{Format, ImageCheckArguments, ImageListArguments, ImageSizeArguments, Pretty},
1212
config::Config,
1313
core::bakefile::{self, Targets, TargetsOptions},
1414
models::{Manifest, TagList},
@@ -70,7 +70,7 @@ pub fn list_images(arguments: ImageListArguments) -> Result<(), Error> {
7070
})
7171
.collect();
7272

73-
print_to_stdout(list, arguments.pretty)
73+
serialize_to_stdout(list, arguments.pretty)
7474
}
7575

7676
pub async fn check_images(arguments: ImageCheckArguments, config: Config) -> Result<(), Error> {
@@ -243,10 +243,35 @@ pub async fn calculate_size(arguments: ImageSizeArguments, config: Config) -> Re
243243
}
244244
}
245245

246-
print_to_stdout(result, Pretty::Always)
246+
match arguments.format {
247+
Format::Plain => {
248+
if let Some(max_width) = result
249+
.images
250+
.iter()
251+
.map(|i| i.0.len())
252+
.max_by(|lhs, rhs| lhs.cmp(rhs))
253+
{
254+
for (image, size) in result.images {
255+
println!(
256+
"{image:max_width$} {size}",
257+
size = humansize::format_size(size, humansize::BINARY)
258+
);
259+
}
260+
261+
println!(
262+
"{total_text:max_width$} {total}",
263+
total_text = "Total",
264+
total = humansize::format_size(result.total, humansize::BINARY)
265+
)
266+
}
267+
268+
Ok(())
269+
}
270+
Format::Json => serialize_to_stdout(&result, arguments.pretty),
271+
}
247272
}
248273

249-
fn print_to_stdout<T>(value: T, pretty: Pretty) -> Result<(), Error>
274+
fn serialize_to_stdout<T>(value: T, pretty: Pretty) -> Result<(), Error>
250275
where
251276
T: Serialize,
252277
{

0 commit comments

Comments
 (0)