@@ -25,15 +25,10 @@ var imageCmd = cli.Command{
2525}
2626
2727var imageCreateCmd = cli.Command {
28- Name : "create" ,
29- Usage : "Pull and convert an OCI image" ,
30- ArgsUsage : "<name>" ,
31- Flags : []cli.Flag {
32- & cli.StringSliceFlag {
33- Name : "tag" ,
34- Usage : "Set image tag key-value pair (KEY=VALUE, can be repeated)" ,
35- },
36- },
28+ Name : "create" ,
29+ Usage : "Pull and convert an OCI image" ,
30+ ArgsUsage : "<name>" ,
31+ Flags : imageCreateFlags (),
3732 Action : handleImageCreate ,
3833 HideHelpCommand : true ,
3934}
@@ -149,24 +144,21 @@ func handleImageList(ctx context.Context, cmd *cli.Command) error {
149144}
150145
151146func handleImageCreate (ctx context.Context , cmd * cli.Command ) error {
147+ return handleImageCreateLike (ctx , cmd , "hypeman image create <name>" , "image create" )
148+ }
149+
150+ func handleImageCreateLike (ctx context.Context , cmd * cli.Command , usageLine , outputLabel string ) error {
152151 args := cmd .Args ().Slice ()
153152 if len (args ) < 1 {
154- return fmt .Errorf ("image name required\n Usage: hypeman image create <name>" )
153+ return fmt .Errorf ("image name required\n Usage: %s" , usageLine )
155154 }
156155
157156 client := hypeman .NewClient (getDefaultRequestOptions (cmd )... )
158157
159- params := hypeman.ImageNewParams {
160- Name : args [0 ],
161- }
162-
163- tags , malformedTags := parseKeyValueSpecs (cmd .StringSlice ("tag" ))
158+ params , malformedTags := buildImageNewParams (args [0 ], cmd .StringSlice ("tag" ))
164159 for _ , malformed := range malformedTags {
165160 fmt .Fprintf (os .Stderr , "Warning: ignoring malformed tag: %s\n " , malformed )
166161 }
167- if len (tags ) > 0 {
168- params .Tags = tags
169- }
170162
171163 var opts []option.RequestOption
172164 if cmd .Root ().Bool ("debug" ) {
@@ -184,7 +176,7 @@ func handleImageCreate(ctx context.Context, cmd *cli.Command) error {
184176 return err
185177 }
186178 obj := gjson .ParseBytes (res )
187- return ShowJSON (os .Stdout , "image create" , obj , format , transform )
179+ return ShowJSON (os .Stdout , outputLabel , obj , format , transform )
188180 }
189181
190182 result , err := client .Images .New (ctx , params , opts ... )
@@ -195,6 +187,26 @@ func handleImageCreate(ctx context.Context, cmd *cli.Command) error {
195187 return nil
196188}
197189
190+ func imageCreateFlags () []cli.Flag {
191+ return []cli.Flag {
192+ & cli.StringSliceFlag {
193+ Name : "tag" ,
194+ Usage : "Set image tag key-value pair (KEY=VALUE, can be repeated)" ,
195+ },
196+ }
197+ }
198+
199+ func buildImageNewParams (name string , tagSpecs []string ) (hypeman.ImageNewParams , []string ) {
200+ params := hypeman.ImageNewParams {Name : name }
201+
202+ tags , malformedTags := parseKeyValueSpecs (tagSpecs )
203+ if len (tags ) > 0 {
204+ params .Tags = tags
205+ }
206+
207+ return params , malformedTags
208+ }
209+
198210func handleImageGet (ctx context.Context , cmd * cli.Command ) error {
199211 args := cmd .Args ().Slice ()
200212 if len (args ) < 1 {
0 commit comments