Skip to content

Conversation

crazy-max
Copy link
Member

@crazy-max crazy-max commented Aug 22, 2025

fixes #3367

Adds format flag to support multiple formats output using docker/cli formatter. verbose flag is now a shorthand for --format=pretty.

Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is what user asked, I think it would be weird if this worked differently than --format in other commands. So it should work with template but also the json shorthand.

@crazy-max
Copy link
Member Author

So it should work with template but also the json shorthand.

Yes you right and also shouldn't be an array, I will change that.

@crazy-max crazy-max added this to the v0.28.0 milestone Aug 25, 2025
@crazy-max crazy-max changed the title cmd: json format support for du command in verbose mode cmd: multiple format output support for du command Aug 25, 2025
@crazy-max crazy-max force-pushed the du-json branch 5 times, most recently from 28196cd to 9af0a0c Compare August 25, 2025 11:42
@crazy-max crazy-max changed the title cmd: multiple format output support for du command cmd: multiple formats output support for du command Aug 25, 2025
Comment on lines +246 to +296
func printKV(w io.Writer, k string, v any) {
fmt.Fprintf(w, "%s:\t%v\n", k, v)
}

func printVerbose(tw *tabwriter.Writer, du []*client.UsageInfo) {
for _, di := range du {
printKV(tw, "ID", di.ID)
if len(di.Parents) != 0 {
printKV(tw, "Parent", strings.Join(di.Parents, ","))
}
printKV(tw, "Created at", di.CreatedAt)
printKV(tw, "Mutable", di.Mutable)
printKV(tw, "Reclaimable", !di.InUse)
printKV(tw, "Shared", di.Shared)
printKV(tw, "Size", units.HumanSize(float64(di.Size)))
if di.Description != "" {
printKV(tw, "Description", di.Description)
}
printKV(tw, "Usage count", di.UsageCount)
if di.LastUsedAt != nil {
printKV(tw, "Last used", units.HumanDuration(time.Since(*di.LastUsedAt))+" ago")
}
if di.RecordType != "" {
printKV(tw, "Type", di.RecordType)
}

fmt.Fprintf(tw, "\n")
}

tw.Flush()
}

func printTableHeader(tw *tabwriter.Writer) {
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tLAST ACCESSED")
}

func printTableRow(tw *tabwriter.Writer, di *client.UsageInfo) {
id := di.ID
if di.Mutable {
id += "*"
}
size := units.HumanSize(float64(di.Size))
if di.Shared {
size += "*"
}
lastAccessed := ""
if di.LastUsedAt != nil {
lastAccessed = units.HumanDuration(time.Since(*di.LastUsedAt)) + " ago"
}
fmt.Fprintf(tw, "%-40s\t%-5v\t%-10s\t%s\n", id, !di.InUse, size, lastAccessed)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved logic not used anymore to prune command for now but we should also consider using docker/cli formatter for this command.

Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do --format=table then I get empty rows, so it seems to know that this input is special but not handle it correctly. This could be follow-up.

crazy-max and others added 2 commits August 27, 2025 18:07
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Comment on lines +81 to +83
} else if opts.format == formatter.TableFormatKey {
opts.format = duDefaultTableFormat
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do --format=table then I get empty rows, so it seems to know that this input is special but not handle it correctly. This could be follow-up.

@tonistiigi fixed here

@thaJeztah
Copy link
Member

we should also consider using docker/cli formatter for this command.

Before you do, please check; we're trying to move many parts in the CLI internal, as there were too many things public that caused us not being able to make changes; for some we could find a new place though.

Also considering if the CLI should execute buildx for performing prune instead of using the moby REST API (as part of docker system prune at least).

@tonistiigi tonistiigi merged commit d94a6cf into docker:master Aug 27, 2025
140 checks passed
@crazy-max crazy-max deleted the du-json branch August 27, 2025 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support --format=json for buildx du --verbose
3 participants