Skip to content

Commit ccca7c7

Browse files
committed
history: json format support for inspect command
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 04aab69 commit ccca7c7

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

commands/history/inspect.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/buildx/util/confutil"
2626
"github.com/docker/buildx/util/desktop"
2727
"github.com/docker/cli/cli/command"
28+
"github.com/docker/cli/cli/command/formatter"
2829
"github.com/docker/cli/cli/debug"
2930
slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
3031
slsa02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2"
@@ -57,6 +58,7 @@ const (
5758
type inspectOptions struct {
5859
builder string
5960
ref string
61+
format string
6062
}
6163

6264
type inspectOutput struct {
@@ -399,6 +401,14 @@ workers0:
399401
})
400402
out.Config.RestRaw = unusedAttrs
401403

404+
if opts.format == formatter.JSONFormatKey {
405+
enc := json.NewEncoder(dockerCli.Out())
406+
enc.SetIndent("", " ")
407+
return enc.Encode(out)
408+
} else if opts.format != formatter.RawFormatKey {
409+
return errors.Errorf("unsupported format %q", opts.format)
410+
}
411+
402412
tw := tabwriter.NewWriter(dockerCli.Out(), 1, 8, 1, '\t', 0)
403413

404414
if out.Context != "" {
@@ -603,7 +613,8 @@ func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
603613
attachmentCmd(dockerCli, rootOpts),
604614
)
605615

606-
// flags := cmd.Flags()
616+
flags := cmd.Flags()
617+
flags.StringVar(&options.format, "format", formatter.RawFormatKey, "Format the output")
607618

608619
return cmd
609620
}

docs/reference/buildx_history_inspect.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,69 @@ Inspect a build
1212

1313
### Options
1414

15-
| Name | Type | Default | Description |
16-
|:----------------|:---------|:--------|:-----------------------------------------|
17-
| `--builder` | `string` | | Override the configured builder instance |
18-
| `-D`, `--debug` | `bool` | | Enable debug logging |
15+
| Name | Type | Default | Description |
16+
|:----------------------|:---------|:--------|:-----------------------------------------|
17+
| `--builder` | `string` | | Override the configured builder instance |
18+
| `-D`, `--debug` | `bool` | | Enable debug logging |
19+
| [`--format`](#format) | `string` | `raw` | Format the output |
1920

2021

2122
<!---MARKER_GEN_END-->
2223

24+
## Examples
25+
26+
### <a name="format"></a> Format the output (--format)
27+
28+
Output format can be one of `raw`, `json`.
29+
30+
```console
31+
$ docker buildx history inspect --format raw
32+
Context: .
33+
Dockerfile: Dockerfile
34+
VCS Repository: https://github.com/crazy-max/buildx.git
35+
VCS Revision: 04aab6958cb5feb012a3c607569573b5cab141e1
36+
Target: binaries
37+
Platforms: linux/amd64
38+
Keep Git Dir: true
39+
40+
Started: 2025-02-06 16:15:13
41+
Duration: 1m 3s
42+
Build Steps: 16/16 (25% cached)
43+
44+
45+
Materials:
46+
URI DIGEST
47+
pkg:docker/docker/dockerfile@1 sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e36d0f104d4d801e66d25
48+
pkg:docker/golang@1.23-alpine3.21?platform=linux%2Famd64 sha256:2c49857f2295e89b23b28386e57e018a86620a8fede5003900f2d138ba9c4037
49+
pkg:docker/tonistiigi/xx@1.6.1?platform=linux%2Famd64 sha256:923441d7c25f1e2eb5789f82d987693c47b8ed987c4ab3b075d6ed2b5d6779a3
50+
51+
Attachments:
52+
DIGEST PLATFORM TYPE
53+
sha256:1b44912514074d3e309d80f8a5886a4d89eeeb52bef4d3e57ced17d1781bfce1 https://slsa.dev/provenance/v0.2
54+
55+
Print build logs: docker buildx history logs qrdbfvaoarfz42ye54lzx9aoy
56+
```
57+
58+
```console
59+
$ docker buildx history inspect --format json
60+
{
61+
"name": "buildx (binaries)",
62+
"context": ".",
63+
"dockerfile": "Dockerfile",
64+
"vcs_repository": "https://github.com/crazy-max/buildx.git",
65+
"vcs_revision": "04aab6958cb5feb012a3c607569573b5cab141e1",
66+
"target": "binaries",
67+
"platform": [
68+
"linux/amd64"
69+
],
70+
"keep_git_dir": true,
71+
"started_at": "2025-02-06T16:15:13.077644732+01:00",
72+
"complete_at": "2025-02-06T16:16:17.046656296+01:00",
73+
"duration": 63969011564,
74+
"status": "completed",
75+
"num_completed_steps": 16,
76+
"num_total_steps": 16,
77+
"num_cached_steps": 4,
78+
"config": {}
79+
}
80+
```

0 commit comments

Comments
 (0)