Skip to content

Commit

Permalink
cmd/oci-image-tool: support multiple --ref
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <[email protected]>
  • Loading branch information
runcom committed Sep 6, 2016
1 parent 55691e3 commit 6ca74be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
20 changes: 10 additions & 10 deletions cmd/oci-image-tool/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type validateCmd struct {
stdout *log.Logger
stderr *log.Logger
typ string // the type to validate, can be empty string
ref string
refs []string
}

func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
Expand All @@ -62,9 +62,9 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
),
)

cmd.Flags().StringVar(
&v.ref, "ref", "v1.0",
`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.`,
cmd.Flags().StringSliceVar(
&v.refs, "ref", []string{"v1.0"},
`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.`,
)

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

func (v *validateCmd) validatePath(name string) error {
var err error
typ := v.typ
var (
err error
typ = v.typ
)

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

switch typ {
case typeImageLayout:
return image.ValidateLayout(name, v.ref)
return image.ValidateLayout(name, v.refs, v.stdout)
case typeImage:
return image.Validate(name, v.ref)
return image.Validate(name, v.refs, v.stdout)
}

f, err := os.Open(name)
Expand All @@ -137,10 +139,8 @@ func (v *validateCmd) validatePath(name string) error {
switch typ {
case typeManifest:
return schema.MediaTypeManifest.Validate(f)

case typeManifestList:
return schema.MediaTypeManifestList.Validate(f)

case typeConfig:
return schema.MediaTypeImageConfig.Validate(f)
}
Expand Down
53 changes: 31 additions & 22 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,57 @@ package image

import (
"encoding/json"
"log"
"os"
"path/filepath"

"github.com/pkg/errors"
)

// ValidateLayout walks through the file tree given by src and
// validates the manifest pointed to by the given ref
// validates the manifest pointed to by the given refs
// or returns an error if the validation failed.
func ValidateLayout(src, ref string) error {
return validate(newPathWalker(src), ref)
func ValidateLayout(src string, refs []string, out *log.Logger) error {
return validate(newPathWalker(src), refs, out)
}

// Validate walks through the given .tar file and
// validates the manifest pointed to by the given ref
// validates the manifest pointed to by the given refs
// or returns an error if the validation failed.
func Validate(tarFile, ref string) error {
func Validate(tarFile string, refs []string, out *log.Logger) error {
f, err := os.Open(tarFile)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
defer f.Close()

return validate(newTarWalker(f), ref)
return validate(newTarWalker(f), refs, out)
}

func validate(w walker, refName string) error {
ref, err := findDescriptor(w, refName)
if err != nil {
return err
}

if err = ref.validate(w); err != nil {
return err
}

m, err := findManifest(w, ref)
if err != nil {
return err
}

return m.validate(w)
func validate(w walker, refs []string, out *log.Logger) error {
for _, r := range refs {
ref, err := findDescriptor(w, r)
if err != nil {
return err
}

if err = ref.validate(w); err != nil {
return err
}

m, err := findManifest(w, ref)
if err != nil {
return err
}

if err := m.validate(w); err != nil {
return err
}
if out != nil {
out.Printf("reference %q: OK", r)
}
}
return nil
}

// UnpackLayout walks through the file tree given by src and
Expand Down

0 comments on commit 6ca74be

Please sign in to comment.