Skip to content

Commit e236b86

Browse files
committed
history: set materials and attachments to json output for inspect
Signed-off-by: CrazyMax <[email protected]>
1 parent 633e8a0 commit e236b86

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

commands/history/inspect.go

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ type inspectOutput struct {
8888

8989
Config configOutput `json:"config,omitempty"`
9090

91+
Materials []materialOutput `json:"materials,omitempty"`
92+
Attachments []attachmentOutput `json:"attachments,omitempty"`
93+
9194
Errors []string `json:"errors,omitempty"`
9295
}
9396

@@ -111,6 +114,17 @@ type configOutput struct {
111114
RestRaw []keyValueOutput `json:"rest_raw,omitempty"`
112115
}
113116

117+
type materialOutput struct {
118+
URI string `json:"uri,omitempty"`
119+
Digests []string `json:"digests,omitempty"`
120+
}
121+
122+
type attachmentOutput struct {
123+
Digest string `json:"digest,omitempty"`
124+
Platform string `json:"platform,omitempty"`
125+
Type string `json:"type,omitempty"`
126+
}
127+
114128
type errorOutput struct {
115129
Code int `json:"code,omitempty"`
116130
Message string `json:"message,omitempty"`
@@ -408,6 +422,46 @@ workers0:
408422
})
409423
out.Config.RestRaw = unusedAttrs
410424

425+
attachments, err := allAttachments(ctx, store, *rec)
426+
if err != nil {
427+
return err
428+
}
429+
430+
provIndex := slices.IndexFunc(attachments, func(a attachment) bool {
431+
return descrType(a.descr) == slsa02.PredicateSLSAProvenance
432+
})
433+
if provIndex != -1 {
434+
prov := attachments[provIndex]
435+
dt, err := content.ReadBlob(ctx, store, prov.descr)
436+
if err != nil {
437+
return errors.Errorf("failed to read provenance %s: %v", prov.descr.Digest, err)
438+
}
439+
var pred provenancetypes.ProvenancePredicate
440+
if err := json.Unmarshal(dt, &pred); err != nil {
441+
return errors.Errorf("failed to unmarshal provenance %s: %v", prov.descr.Digest, err)
442+
}
443+
for _, m := range pred.Materials {
444+
out.Materials = append(out.Materials, materialOutput{
445+
URI: m.URI,
446+
Digests: digestSetToDigests(m.Digest),
447+
})
448+
}
449+
}
450+
451+
if len(attachments) > 0 {
452+
for _, a := range attachments {
453+
p := ""
454+
if a.platform != nil {
455+
p = platforms.FormatAll(*a.platform)
456+
}
457+
out.Attachments = append(out.Attachments, attachmentOutput{
458+
Digest: a.descr.Digest.String(),
459+
Platform: p,
460+
Type: descrType(a.descr),
461+
})
462+
}
463+
}
464+
411465
if opts.format == formatter.JSONFormatKey {
412466
enc := json.NewEncoder(dockerCli.Out())
413467
enc.SetIndent("", " ")
@@ -526,47 +580,23 @@ workers0:
526580
printTable(dockerCli.Out(), out.BuildArgs, "Build Arg")
527581
printTable(dockerCli.Out(), out.Labels, "Label")
528582

529-
attachments, err := allAttachments(ctx, store, *rec)
530-
if err != nil {
531-
return err
532-
}
533-
534-
provIndex := slices.IndexFunc(attachments, func(a attachment) bool {
535-
return descrType(a.descr) == slsa02.PredicateSLSAProvenance
536-
})
537-
if provIndex != -1 {
538-
prov := attachments[provIndex]
539-
540-
dt, err := content.ReadBlob(ctx, store, prov.descr)
541-
if err != nil {
542-
return errors.Errorf("failed to read provenance %s: %v", prov.descr.Digest, err)
543-
}
544-
545-
var pred provenancetypes.ProvenancePredicate
546-
if err := json.Unmarshal(dt, &pred); err != nil {
547-
return errors.Errorf("failed to unmarshal provenance %s: %v", prov.descr.Digest, err)
548-
}
549-
583+
if len(out.Materials) > 0 {
550584
fmt.Fprintln(dockerCli.Out(), "Materials:")
551585
tw = tabwriter.NewWriter(dockerCli.Out(), 1, 8, 1, '\t', 0)
552586
fmt.Fprintf(tw, "URI\tDIGEST\n")
553-
for _, m := range pred.Materials {
554-
fmt.Fprintf(tw, "%s\t%s\n", m.URI, strings.Join(digestSetToDigests(m.Digest), ", "))
587+
for _, m := range out.Materials {
588+
fmt.Fprintf(tw, "%s\t%s\n", m.URI, strings.Join(m.Digests, ", "))
555589
}
556590
tw.Flush()
557591
fmt.Fprintln(dockerCli.Out())
558592
}
559593

560-
if len(attachments) > 0 {
594+
if len(out.Attachments) > 0 {
561595
fmt.Fprintf(tw, "Attachments:\n")
562596
tw = tabwriter.NewWriter(dockerCli.Out(), 1, 8, 1, '\t', 0)
563597
fmt.Fprintf(tw, "DIGEST\tPLATFORM\tTYPE\n")
564-
for _, a := range attachments {
565-
p := ""
566-
if a.platform != nil {
567-
p = platforms.FormatAll(*a.platform)
568-
}
569-
fmt.Fprintf(tw, "%s\t%s\t%s\n", a.descr.Digest, p, descrType(a.descr))
598+
for _, a := range out.Attachments {
599+
fmt.Fprintf(tw, "%s\t%s\t%s\n", a.Digest, a.Platform, a.Type)
570600
}
571601
tw.Flush()
572602
fmt.Fprintln(dockerCli.Out())

0 commit comments

Comments
 (0)