@@ -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
6465type 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
97100type 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
117120type 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
122125type 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
128131type 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
137140type 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
142145func 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 }
0 commit comments