Skip to content

Commit ff67ec5

Browse files
committed
oci-image-tool: address review comments
Signed-off-by: Sergiusz Urbaniak <[email protected]>
1 parent e0a8650 commit ff67ec5

9 files changed

Lines changed: 413 additions & 139 deletions

File tree

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ media-types.png: media-types.dot
7070
%.png: %.dot
7171
dot -Tpng $^ > $@
7272

73-
fs:
74-
esc -o schema/fs.go -pkg="schema" -ignore=".*go" schema
75-
7673
.PHONY: \
7774
validate-examples \
7875
lint

cmd/oci-image-tool/validate.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"os"
1111
"strings"
1212

13-
"github.com/opencontainers/image-spec/oci"
1413
"github.com/opencontainers/image-spec/schema"
1514
"github.com/pkg/errors"
1615
"github.com/spf13/cobra"
1716
)
1817

18+
// supported validation types
1919
const (
2020
typeImageLayout = "imageLayout"
2121
typeImage = "image"
@@ -80,7 +80,7 @@ func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
8080
}
8181

8282
var errs []error
83-
if verr, ok := errors.Cause(err).(oci.ValidationError); ok {
83+
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
8484
errs = verr.Errs
8585
} else {
8686
v.stderr.Printf("file %s: validation failed: %v", arg, err)
@@ -108,15 +108,21 @@ func (v *validateCmd) validatePath(name string) error {
108108
}
109109
}
110110

111+
f, err := os.Open(name)
112+
if err != nil {
113+
return errors.Wrap(err, "unable to open file")
114+
}
115+
defer f.Close()
116+
111117
switch typ {
112118
case typeManifest:
113-
if err := withFile(name, oci.ValidateManifest); err != nil {
119+
if err := schema.MediaTypeManifest.Validate(f); err != nil {
114120
return err
115121
}
116122

117123
return nil
118124
case typeManifestList:
119-
if err := withFile(name, oci.ValidateManifestList); err != nil {
125+
if err := schema.MediaTypeManifestList.Validate(f); err != nil {
120126
return err
121127
}
122128

@@ -126,20 +132,8 @@ func (v *validateCmd) validatePath(name string) error {
126132
return fmt.Errorf("type %q unimplemented", typ)
127133
}
128134

129-
// withFile opens the named file for reading and executes the given function fn if no error occured.
130-
// If the given function returns an error it will be returned.
131-
func withFile(name string, fn func(f io.Reader) error) error {
132-
f, err := os.Open(name)
133-
if err != nil {
134-
return errors.Wrap(err, "unable to open file")
135-
}
136-
defer f.Close()
137-
138-
return fn(f)
139-
}
140-
141-
// autodetect returns one of the type.. constants of the content of the given path
142-
// or an error if the type could not be resolved.
135+
// autodetect detects the validation type for the given path
136+
// or an error if the validation type could not be resolved.
143137
func autodetect(path string) (string, error) {
144138
fi, err := os.Stat(path)
145139
if err != nil {
@@ -156,7 +150,7 @@ func autodetect(path string) (string, error) {
156150
}
157151
defer f.Close()
158152

159-
buf, err := ioutil.ReadAll(io.LimitReader(f, 512)) // read up to 512 bytes for content detection
153+
buf, err := ioutil.ReadAll(io.LimitReader(f, 512)) // read some initial bytes to detect content
160154
if err != nil {
161155
return "", errors.Wrap(err, "unable to read")
162156
}
@@ -192,10 +186,10 @@ func autodetect(path string) (string, error) {
192186
}
193187

194188
switch {
195-
case header.MediaType == schema.MediaTypeManifest:
189+
case header.MediaType == string(schema.MediaTypeManifest):
196190
return typeManifest, nil
197191

198-
case header.MediaType == schema.MediaTypeManifestList:
192+
case header.MediaType == string(schema.MediaTypeManifestList):
199193
return typeManifestList, nil
200194

201195
case header.MediaType == "" && header.SchemaVersion == 0 && header.Config != nil:

cmd/oci-validate-examples/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"strings"
1515

16+
"github.com/opencontainers/image-spec/schema"
1617
"github.com/russross/blackfriday"
1718
"github.com/xeipuuv/gojsonschema"
1819
)
@@ -93,18 +94,13 @@ func main() {
9394
}
9495

9596
var (
96-
specsByMediaType = map[string]string{
97-
"application/vnd.oci.image.manifest.v1+json": "image-manifest-schema.json",
98-
"application/vnd.oci.image.manifest.list.v1+json": "manifest-list-schema.json",
99-
}
100-
10197
errSchemaNotFound = errors.New("schema: not found")
10298
errFormatInvalid = errors.New("format: invalid")
10399
)
104100

105101
func schemaByMediatype(root, mediatype string) (gojsonschema.JSONLoader, error) {
106-
name, ok := specsByMediaType[mediatype]
107-
if !ok {
102+
name := schema.File(mediatype)
103+
if name == "" {
108104
return nil, errSchemaNotFound
109105
}
110106

oci/doc.go

Lines changed: 0 additions & 2 deletions
This file was deleted.

oci/validation.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

schema/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// Package schema defines the media types as well as the schema definitions.
1+
// Package schema defines the OCI image media types, schema definitions and validation functions.
22
package schema

0 commit comments

Comments
 (0)