Skip to content

Commit 4f6b5fd

Browse files
authored
Merge pull request #265 from runcom/multi-ref
cmd/oci-image-tool: support multiple --ref
2 parents f36d5c5 + 6ca74be commit 4f6b5fd

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

cmd/oci-image-tool/validate.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type validateCmd struct {
3939
stdout *log.Logger
4040
stderr *log.Logger
4141
typ string // the type to validate, can be empty string
42-
ref string
42+
refs []string
4343
}
4444

4545
func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
@@ -62,9 +62,9 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
6262
),
6363
)
6464

65-
cmd.Flags().StringVar(
66-
&v.ref, "ref", "v1.0",
67-
`The ref pointing to the manifest to be validated. This must be present in the "refs" subdirectory of the image. Only applicable if type is image or imageLayout.`,
65+
cmd.Flags().StringSliceVar(
66+
&v.refs, "ref", []string{"v1.0"},
67+
`A set of refs pointing to the manifests to be validated. Each reference must be present in the "refs" subdirectory of the image. Only applicable if type is image or imageLayout.`,
6868
)
6969

7070
return cmd
@@ -112,8 +112,10 @@ func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
112112
}
113113

114114
func (v *validateCmd) validatePath(name string) error {
115-
var err error
116-
typ := v.typ
115+
var (
116+
err error
117+
typ = v.typ
118+
)
117119

118120
if typ == "" {
119121
if typ, err = autodetect(name); err != nil {
@@ -123,9 +125,9 @@ func (v *validateCmd) validatePath(name string) error {
123125

124126
switch typ {
125127
case typeImageLayout:
126-
return image.ValidateLayout(name, v.ref)
128+
return image.ValidateLayout(name, v.refs, v.stdout)
127129
case typeImage:
128-
return image.Validate(name, v.ref)
130+
return image.Validate(name, v.refs, v.stdout)
129131
}
130132

131133
f, err := os.Open(name)
@@ -137,10 +139,8 @@ func (v *validateCmd) validatePath(name string) error {
137139
switch typ {
138140
case typeManifest:
139141
return schema.MediaTypeManifest.Validate(f)
140-
141142
case typeManifestList:
142143
return schema.MediaTypeManifestList.Validate(f)
143-
144144
case typeConfig:
145145
return schema.MediaTypeImageConfig.Validate(f)
146146
}

image/image.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,57 @@ package image
1616

1717
import (
1818
"encoding/json"
19+
"log"
1920
"os"
2021
"path/filepath"
2122

2223
"github.com/pkg/errors"
2324
)
2425

2526
// ValidateLayout walks through the file tree given by src and
26-
// validates the manifest pointed to by the given ref
27+
// validates the manifest pointed to by the given refs
2728
// or returns an error if the validation failed.
28-
func ValidateLayout(src, ref string) error {
29-
return validate(newPathWalker(src), ref)
29+
func ValidateLayout(src string, refs []string, out *log.Logger) error {
30+
return validate(newPathWalker(src), refs, out)
3031
}
3132

3233
// Validate walks through the given .tar file and
33-
// validates the manifest pointed to by the given ref
34+
// validates the manifest pointed to by the given refs
3435
// or returns an error if the validation failed.
35-
func Validate(tarFile, ref string) error {
36+
func Validate(tarFile string, refs []string, out *log.Logger) error {
3637
f, err := os.Open(tarFile)
3738
if err != nil {
3839
return errors.Wrap(err, "unable to open file")
3940
}
4041
defer f.Close()
4142

42-
return validate(newTarWalker(f), ref)
43+
return validate(newTarWalker(f), refs, out)
4344
}
4445

45-
func validate(w walker, refName string) error {
46-
ref, err := findDescriptor(w, refName)
47-
if err != nil {
48-
return err
49-
}
50-
51-
if err = ref.validate(w); err != nil {
52-
return err
53-
}
54-
55-
m, err := findManifest(w, ref)
56-
if err != nil {
57-
return err
58-
}
59-
60-
return m.validate(w)
46+
func validate(w walker, refs []string, out *log.Logger) error {
47+
for _, r := range refs {
48+
ref, err := findDescriptor(w, r)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if err = ref.validate(w); err != nil {
54+
return err
55+
}
56+
57+
m, err := findManifest(w, ref)
58+
if err != nil {
59+
return err
60+
}
61+
62+
if err := m.validate(w); err != nil {
63+
return err
64+
}
65+
if out != nil {
66+
out.Printf("reference %q: OK", r)
67+
}
68+
}
69+
return nil
6170
}
6271

6372
// UnpackLayout walks through the file tree given by src and

0 commit comments

Comments
 (0)