Skip to content

Commit 4afbd68

Browse files
committed
cmd: json format support for du command in verbose mode
Signed-off-by: CrazyMax <[email protected]>
1 parent a6e198a commit 4afbd68

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

commands/diskusage.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package commands
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"os"
9+
"slices"
810
"strings"
911
"text/tabwriter"
1012
"time"
@@ -13,9 +15,11 @@ import (
1315
"github.com/docker/buildx/util/cobrautil/completion"
1416
"github.com/docker/cli/cli"
1517
"github.com/docker/cli/cli/command"
18+
"github.com/docker/cli/cli/command/formatter"
1619
"github.com/docker/cli/opts"
1720
"github.com/docker/go-units"
1821
"github.com/moby/buildkit/client"
22+
"github.com/pkg/errors"
1923
"github.com/spf13/cobra"
2024
"golang.org/x/sync/errgroup"
2125
)
@@ -24,9 +28,19 @@ type duOptions struct {
2428
builder string
2529
filter opts.FilterOpt
2630
verbose bool
31+
format string
2732
}
2833

2934
func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) error {
35+
if opts.format != "" {
36+
if !opts.verbose {
37+
return errors.New("cannot use --format without --verbose")
38+
}
39+
if !slices.Contains([]string{formatter.RawFormatKey, formatter.JSONFormatKey}, opts.format) {
40+
return errors.Errorf("unsupported format: %q", opts.format)
41+
}
42+
}
43+
3044
pi, err := toBuildkitPruneInfo(opts.filter.Value())
3145
if err != nil {
3246
return err
@@ -74,6 +88,18 @@ func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) er
7488
return err
7589
}
7690

91+
if opts.verbose && opts.format == formatter.JSONFormatKey {
92+
var dus []*client.UsageInfo
93+
for _, du := range out {
94+
if du != nil {
95+
dus = append(dus, du...)
96+
}
97+
}
98+
enc := json.NewEncoder(dockerCli.Out())
99+
enc.SetIndent("", " ")
100+
return enc.Encode(dus)
101+
}
102+
77103
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
78104
first := true
79105
for _, du := range out {
@@ -120,6 +146,7 @@ func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
120146
flags := cmd.Flags()
121147
flags.Var(&options.filter, "filter", "Provide filter values")
122148
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
149+
flags.StringVar(&options.format, "format", "", "Format the output in verbose mode")
123150

124151
return cmd
125152
}

docs/reference/buildx_du.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Disk usage
1414
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
1515
| `-D`, `--debug` | `bool` | | Enable debug logging |
1616
| `--filter` | `filter` | | Provide filter values |
17+
| [`--format`](#format) | `string` | | Format the output in verbose mode |
1718
| [`--verbose`](#verbose) | `bool` | | Provide a more verbose output |
1819

1920

@@ -117,3 +118,41 @@ uj802yxtvkcjysnjb4kgwvn2v true 11.68MB 45 hours ago
117118
Reclaimable: 2.627GB
118119
Total: 2.627GB
119120
```
121+
122+
### <a name="format"></a> Format the output in verbose mode (--format)
123+
124+
The formatting options (`--format`) can be used with [`--verbose` flag](#verbose)
125+
to print the output in `raw` (default) or `json`:
126+
127+
```console
128+
$ docker buildx du --verbose --format=json
129+
[
130+
...
131+
{
132+
"id": "4m8061kctvjyh9qleus8rgpgx",
133+
"mutable": true,
134+
"inUse": false,
135+
"size": 1723305984,
136+
"createdAt": "2025-06-19T21:00:41.53563755Z",
137+
"lastUsedAt": "2025-08-11T09:20:22.598927091Z",
138+
"usageCount": 25,
139+
"parents": null,
140+
"description": "cached mount /root/.cache/go-build from exec /bin/sh -c set -ex\n go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./...\n go tool cover -func=/tmp/coverage.txt\n with id \"//root/.cache/go-build\"",
141+
"recordType": "exec.cachemount",
142+
"shared": false
143+
},
144+
{
145+
"id": "fcm9mlz2641u8r5eicjqdhy1l",
146+
"mutable": false,
147+
"inUse": false,
148+
"size": 1840796514,
149+
"createdAt": "2025-06-21T12:45:07.224976569Z",
150+
"lastUsedAt": "2025-06-21T12:45:59.043699703Z",
151+
"usageCount": 1,
152+
"parents": null,
153+
"description": "[stage-35 1/1] COPY --link --from=build /out /",
154+
"recordType": "regular",
155+
"shared": true
156+
}
157+
]
158+
```

0 commit comments

Comments
 (0)