@@ -18,7 +18,6 @@ import (
18
18
"fmt"
19
19
20
20
"github.com/google/go-containerregistry/pkg/crane"
21
- v1 "github.com/google/go-containerregistry/pkg/v1"
22
21
"github.com/google/go-containerregistry/pkg/v1/tarball"
23
22
"github.com/google/go-containerregistry/pkg/v1/validate"
24
23
"github.com/spf13/cobra"
@@ -36,28 +35,56 @@ func NewCmdValidate(options *[]crane.Option) *cobra.Command {
36
35
Short : "Validate that an image is well-formed" ,
37
36
Args : cobra .ExactArgs (0 ),
38
37
RunE : func (cmd * cobra.Command , args []string ) error {
39
- for flag , maker := range map [string ]func (string , ... crane.Option ) (v1.Image , error ){
40
- tarballPath : makeTarball ,
41
- remoteRef : crane .Pull ,
42
- } {
43
- if flag == "" {
44
- continue
45
- }
46
- img , err := maker (flag , * options ... )
38
+ if tarballPath != "" {
39
+ img , err := tarball .ImageFromPath (tarballPath , nil )
47
40
if err != nil {
48
- return fmt .Errorf ("failed to read image %s: %w" , flag , err )
41
+ return fmt .Errorf ("failed to read image %s: %w" , tarballPath , err )
49
42
}
50
-
51
43
opt := []validate.Option {}
52
44
if fast {
53
45
opt = append (opt , validate .Fast )
54
46
}
55
47
if err := validate .Image (img , opt ... ); err != nil {
56
- fmt .Fprintf (cmd .OutOrStdout (), "FAIL: %s: %v\n " , flag , err )
48
+ fmt .Fprintf (cmd .OutOrStdout (), "FAIL: %s: %v\n " , tarballPath , err )
57
49
return err
58
50
}
59
- fmt .Fprintf (cmd .OutOrStdout (), "PASS: %s\n " , flag )
51
+ fmt .Fprintf (cmd .OutOrStdout (), "PASS: %s\n " , tarballPath )
52
+ }
53
+
54
+ if remoteRef != "" {
55
+ rmt , err := crane .Get (remoteRef , * options ... )
56
+ if err != nil {
57
+ return fmt .Errorf ("failed to read image %s: %w" , remoteRef , err )
58
+ }
59
+
60
+ o := crane .GetOptions (* options ... )
61
+
62
+ opt := []validate.Option {}
63
+ if fast {
64
+ opt = append (opt , validate .Fast )
65
+ }
66
+ if rmt .MediaType .IsIndex () && o .Platform == nil {
67
+ idx , err := rmt .ImageIndex ()
68
+ if err != nil {
69
+ return fmt .Errorf ("reading index: %w" , err )
70
+ }
71
+ if err := validate .Index (idx , opt ... ); err != nil {
72
+ fmt .Fprintf (cmd .OutOrStdout (), "FAIL: %s: %v\n " , remoteRef , err )
73
+ return err
74
+ }
75
+ } else {
76
+ img , err := rmt .Image ()
77
+ if err != nil {
78
+ return fmt .Errorf ("reading image: %w" , err )
79
+ }
80
+ if err := validate .Image (img , opt ... ); err != nil {
81
+ fmt .Fprintf (cmd .OutOrStdout (), "FAIL: %s: %v\n " , remoteRef , err )
82
+ return err
83
+ }
84
+ }
85
+ fmt .Fprintf (cmd .OutOrStdout (), "PASS: %s\n " , remoteRef )
60
86
}
87
+
61
88
return nil
62
89
},
63
90
}
@@ -67,7 +94,3 @@ func NewCmdValidate(options *[]crane.Option) *cobra.Command {
67
94
68
95
return validateCmd
69
96
}
70
-
71
- func makeTarball (path string , _ ... crane.Option ) (v1.Image , error ) {
72
- return tarball .ImageFromPath (path , nil )
73
- }
0 commit comments