Skip to content

Commit 417af36

Browse files
committed
history: support go template format for inspect
Signed-off-by: CrazyMax <[email protected]>
1 parent e236b86 commit 417af36

File tree

2 files changed

+134
-77
lines changed

2 files changed

+134
-77
lines changed

commands/history/inspect.go

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strconv"
1414
"strings"
1515
"text/tabwriter"
16+
"text/template"
1617
"time"
1718

1819
"github.com/containerd/containerd/v2/core/content"
@@ -62,81 +63,83 @@ type inspectOptions struct {
6263
}
6364

6465
type inspectOutput struct {
65-
Name string `json:"name,omitempty"`
66-
Context string `json:"context,omitempty"`
67-
Dockerfile string `json:"dockerfile,omitempty"`
68-
VCSRepository string `json:"vcs_repository,omitempty"`
69-
VCSRevision string `json:"vcs_revision,omitempty"`
70-
Target string `json:"target,omitempty"`
71-
Platform []string `json:"platform,omitempty"`
72-
KeepGitDir bool `json:"keep_git_dir,omitempty"`
66+
Name string `json:",omitempty"`
67+
Ref string
7368

74-
NamedContexts []keyValueOutput `json:"named_contexts,omitempty"`
69+
Context string `json:",omitempty"`
70+
Dockerfile string `json:",omitempty"`
71+
VCSRepository string `json:",omitempty"`
72+
VCSRevision string `json:",omitempty"`
73+
Target string `json:",omitempty"`
74+
Platform []string `json:",omitempty"`
75+
KeepGitDir bool `json:",omitempty"`
7576

76-
StartedAt *time.Time `json:"started_at,omitempty"`
77-
CompletedAt *time.Time `json:"complete_at,omitempty"`
78-
Duration time.Duration `json:"duration,omitempty"`
79-
Status statusT `json:"status,omitempty"`
80-
Error *errorOutput `json:"error,omitempty"`
77+
NamedContexts []keyValueOutput `json:",omitempty"`
8178

82-
NumCompletedSteps int32 `json:"num_completed_steps"`
83-
NumTotalSteps int32 `json:"num_total_steps"`
84-
NumCachedSteps int32 `json:"num_cached_steps"`
79+
StartedAt *time.Time `json:",omitempty"`
80+
CompletedAt *time.Time `json:",omitempty"`
81+
Duration time.Duration `json:",omitempty"`
82+
Status statusT `json:",omitempty"`
83+
Error *errorOutput `json:",omitempty"`
8584

86-
BuildArgs []keyValueOutput `json:"build_args,omitempty"`
87-
Labels []keyValueOutput `json:"labels,omitempty"`
85+
NumCompletedSteps int32
86+
NumTotalSteps int32
87+
NumCachedSteps int32
8888

89-
Config configOutput `json:"config,omitempty"`
89+
BuildArgs []keyValueOutput `json:",omitempty"`
90+
Labels []keyValueOutput `json:",omitempty"`
9091

91-
Materials []materialOutput `json:"materials,omitempty"`
92-
Attachments []attachmentOutput `json:"attachments,omitempty"`
92+
Config configOutput `json:",omitempty"`
9393

94-
Errors []string `json:"errors,omitempty"`
94+
Materials []materialOutput `json:",omitempty"`
95+
Attachments []attachmentOutput `json:",omitempty"`
96+
97+
Errors []string `json:",omitempty"`
9598
}
9699

97100
type configOutput struct {
98-
Network string `json:"network,omitempty"`
99-
ExtraHosts []string `json:"extra_hosts,omitempty"`
100-
Hostname string `json:"hostname,omitempty"`
101-
CgroupParent string `json:"cgroup_parent,omitempty"`
102-
ImageResolveMode string `json:"image_resolve_mode,omitempty"`
103-
MultiPlatform bool `json:"multi_platform,omitempty"`
104-
NoCache bool `json:"no_cache,omitempty"`
105-
NoCacheFilter []string `json:"no_cache_filter,omitempty"`
106-
107-
ShmSize string `json:"shm_size,omitempty"`
108-
Ulimit string `json:"ulimit,omitempty"`
109-
CacheMountNS string `json:"cache_mount_ns,omitempty"`
110-
DockerfileCheckConfig string `json:"dockerfile_check_config,omitempty"`
111-
SourceDateEpoch string `json:"source_date_epoch,omitempty"`
112-
SandboxHostname string `json:"sandbox_hostname,omitempty"`
113-
114-
RestRaw []keyValueOutput `json:"rest_raw,omitempty"`
101+
Network string `json:",omitempty"`
102+
ExtraHosts []string `json:",omitempty"`
103+
Hostname string `json:",omitempty"`
104+
CgroupParent string `json:",omitempty"`
105+
ImageResolveMode string `json:",omitempty"`
106+
MultiPlatform bool `json:",omitempty"`
107+
NoCache bool `json:",omitempty"`
108+
NoCacheFilter []string `json:",omitempty"`
109+
110+
ShmSize string `json:",omitempty"`
111+
Ulimit string `json:",omitempty"`
112+
CacheMountNS string `json:",omitempty"`
113+
DockerfileCheckConfig string `json:",omitempty"`
114+
SourceDateEpoch string `json:",omitempty"`
115+
SandboxHostname string `json:",omitempty"`
116+
117+
RestRaw []keyValueOutput `json:",omitempty"`
115118
}
116119

117120
type materialOutput struct {
118-
URI string `json:"uri,omitempty"`
119-
Digests []string `json:"digests,omitempty"`
121+
URI string `json:",omitempty"`
122+
Digests []string `json:",omitempty"`
120123
}
121124

122125
type attachmentOutput struct {
123-
Digest string `json:"digest,omitempty"`
124-
Platform string `json:"platform,omitempty"`
125-
Type string `json:"type,omitempty"`
126+
Digest string `json:",omitempty"`
127+
Platform string `json:",omitempty"`
128+
Type string `json:",omitempty"`
126129
}
127130

128131
type errorOutput struct {
129-
Code int `json:"code,omitempty"`
130-
Message string `json:"message,omitempty"`
131-
Name string `json:"name,omitempty"`
132-
Logs []string `json:"logs,omitempty"`
133-
Sources []byte `json:"sources,omitempty"`
134-
Stack []byte `json:"stack,omitempty"`
132+
Code int `json:",omitempty"`
133+
Message string `json:",omitempty"`
134+
Name string `json:",omitempty"`
135+
Logs []string `json:",omitempty"`
136+
Sources []byte `json:",omitempty"`
137+
Stack []byte `json:",omitempty"`
135138
}
136139

137140
type keyValueOutput struct {
138-
Name string `json:"name,omitempty"`
139-
Value string `json:"value,omitempty"`
141+
Name string `json:",omitempty"`
142+
Value string `json:",omitempty"`
140143
}
141144

142145
func readAttr[T any](attrs map[string]string, k string, dest *T, f func(v string) (T, bool)) {
@@ -259,6 +262,8 @@ workers0:
259262
delete(attrs, "filename")
260263

261264
out.Name = buildName(rec.FrontendAttrs, st)
265+
out.Ref = rec.Ref
266+
262267
out.Context = context
263268
out.Dockerfile = dockerfile
264269

@@ -467,11 +472,26 @@ workers0:
467472
enc.SetIndent("", " ")
468473
return enc.Encode(out)
469474
} else if opts.format != formatter.RawFormatKey {
470-
return errors.Errorf("unsupported format %q", opts.format)
475+
tmpl, err := template.New("inspect").Parse(opts.format)
476+
if err != nil {
477+
return errors.Wrapf(err, "failed to parse format template")
478+
}
479+
var buf bytes.Buffer
480+
if err := tmpl.Execute(&buf, out); err != nil {
481+
return errors.Wrapf(err, "failed to execute format template")
482+
}
483+
fmt.Fprintln(dockerCli.Out(), buf.String())
484+
return nil
471485
}
472486

473487
tw := tabwriter.NewWriter(dockerCli.Out(), 1, 8, 1, '\t', 0)
474488

489+
if out.Name != "" {
490+
fmt.Fprintf(tw, "Name:\t%s\n", out.Name)
491+
}
492+
if opts.ref == "" && out.Ref != "" {
493+
fmt.Fprintf(tw, "Ref:\t%s\n", out.Ref)
494+
}
475495
if out.Context != "" {
476496
fmt.Fprintf(tw, "Context:\t%s\n", out.Context)
477497
}

docs/reference/buildx_history_inspect.md

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@ Inspect a build
2525

2626
### <a name="format"></a> Format the output (--format)
2727

28-
Output format can be one of `raw`, `json`.
28+
The formatting options (`--format`) pretty-prints the output to `raw` (default),
29+
`json` or using a Go template.
2930

3031
```console
31-
$ docker buildx history inspect --format raw
32+
$ docker buildx history inspect
33+
Name: buildx (binaries)
3234
Context: .
3335
Dockerfile: Dockerfile
3436
VCS Repository: https://github.com/crazy-max/buildx.git
35-
VCS Revision: 04aab6958cb5feb012a3c607569573b5cab141e1
37+
VCS Revision: f15eaa1ee324ffbbab29605600d27a84cab86361
3638
Target: binaries
3739
Platforms: linux/amd64
3840
Keep Git Dir: true
3941

40-
Started: 2025-02-06 16:15:13
41-
Duration: 1m 3s
42+
Started: 2025-02-07 11:56:24
43+
Duration: 1m 1s
4244
Build Steps: 16/16 (25% cached)
4345

46+
Image Resolve Mode: local
4447

4548
Materials:
4649
URI DIGEST
@@ -50,31 +53,65 @@ pkg:docker/tonistiigi/[email protected]?platform=linux%2Famd64 sha256:923441d7c
5053

5154
Attachments:
5255
DIGEST PLATFORM TYPE
53-
sha256:1b44912514074d3e309d80f8a5886a4d89eeeb52bef4d3e57ced17d1781bfce1 https://slsa.dev/provenance/v0.2
56+
sha256:217329d2af959d4f02e3a96dcbe62bf100cab1feb8006a047ddfe51a5397f7e3 https://slsa.dev/provenance/v0.2
5457

55-
Print build logs: docker buildx history logs qrdbfvaoarfz42ye54lzx9aoy
58+
Print build logs: docker buildx history logs g9808bwrjrlkbhdamxklx660b
5659
```
5760

5861
```console
5962
$ docker buildx history inspect --format json
6063
{
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": [
64+
"Name": "buildx (binaries)",
65+
"Ref": "5w7vkqfi0rf59hw4hnmn627r9",
66+
"Context": ".",
67+
"Dockerfile": "Dockerfile",
68+
"VCSRepository": "https://github.com/crazy-max/buildx.git",
69+
"VCSRevision": "f15eaa1ee324ffbbab29605600d27a84cab86361",
70+
"Target": "binaries",
71+
"Platform": [
6872
"linux/amd64"
6973
],
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": {}
74+
"KeepGitDir": true,
75+
"StartedAt": "2025-02-07T12:01:05.75807272+01:00",
76+
"CompletedAt": "2025-02-07T12:02:07.991778875+01:00",
77+
"Duration": 62233706155,
78+
"Status": "completed",
79+
"NumCompletedSteps": 16,
80+
"NumTotalSteps": 16,
81+
"NumCachedSteps": 4,
82+
"Config": {
83+
"ImageResolveMode": "local"
84+
},
85+
"Materials": [
86+
{
87+
"URI": "pkg:docker/docker/dockerfile@1",
88+
"Digests": [
89+
"sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e36d0f104d4d801e66d25"
90+
]
91+
},
92+
{
93+
"URI": "pkg:docker/[email protected]?platform=linux%2Famd64",
94+
"Digests": [
95+
"sha256:2c49857f2295e89b23b28386e57e018a86620a8fede5003900f2d138ba9c4037"
96+
]
97+
},
98+
{
99+
"URI": "pkg:docker/tonistiigi/[email protected]?platform=linux%2Famd64",
100+
"Digests": [
101+
"sha256:923441d7c25f1e2eb5789f82d987693c47b8ed987c4ab3b075d6ed2b5d6779a3"
102+
]
103+
}
104+
],
105+
"Attachments": [
106+
{
107+
"Digest": "sha256:450fdd2e6b868fecd69e9891c2c404ba461aa38a47663b4805edeb8d2baf80b1",
108+
"Type": "https://slsa.dev/provenance/v0.2"
109+
}
110+
]
79111
}
80112
```
113+
114+
```console
115+
$ docker buildx history inspect --format "{{.Name}}: {{.VCSRepository}} ({{.VCSRevision}})"
116+
buildx (binaries): https://github.com/crazy-max/buildx.git (f15eaa1ee324ffbbab29605600d27a84cab86361)
117+
```

0 commit comments

Comments
 (0)