@@ -2,6 +2,9 @@ package main
22
33import (
44 "fmt"
5+ "reflect"
6+ "strings"
7+
58 "github.com/containers/storage"
69 "github.com/kubernetes-incubator/cri-o/cmd/kpod/formats"
710 libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
@@ -10,14 +13,6 @@ import (
1013 "github.com/urfave/cli"
1114)
1215
13- type imageOutputParams struct {
14- ID string `json:"id"`
15- Name string `json:"names"`
16- Digest digest.Digest `json:"digest"`
17- CreatedAt string `json:"created"`
18- Size string `json:"size"`
19- }
20-
2116var (
2217 imagesFlags = []cli.Flag {
2318 cli.BoolFlag {
5752 }
5853)
5954
60- type stdoutstruct struct {
61- output []imageOutputParams
62- truncate , digests , quiet , noheading bool
63- }
64-
65- func (so stdoutstruct ) Out () error {
66- if len (so .output ) > 0 && ! so .noheading && ! so .quiet {
67- outputHeader (so .truncate , so .digests )
68- }
69- lastID := ""
70- for _ , img := range so .output {
71- if so .quiet {
72- if lastID == img .ID {
73- continue // quiet should not show the same ID multiple times.
74- }
75- fmt .Printf ("%-64s\n " , img .ID )
76- continue
77- }
78- if so .truncate {
79- fmt .Printf ("%-20.12s %-56s" , img .ID , img .Name )
80- } else {
81- fmt .Printf ("%-64s %-56s" , img .ID , img .Name )
82- }
83-
84- if so .digests {
85- fmt .Printf (" %-64s" , img .Digest )
86- }
87- fmt .Printf (" %-22s %s\n " , img .CreatedAt , img .Size )
88-
89- }
90- return nil
91- }
92-
93- func toGeneric (params []imageOutputParams ) []interface {} {
94- genericParams := make ([]interface {}, len (params ))
95- for i , v := range params {
96- genericParams [i ] = interface {}(v )
97- }
98- return genericParams
99- }
100-
10155func imagesCmd (c * cli.Context ) error {
10256 config , err := getConfig (c )
10357 if err != nil {
@@ -124,7 +78,7 @@ func imagesCmd(c *cli.Context) error {
12478 if c .IsSet ("digests" ) {
12579 digests = c .Bool ("digests" )
12680 }
127- outputFormat := ""
81+ outputFormat := genImagesFormat ( quiet , truncate , digests )
12882 if c .IsSet ("format" ) {
12983 outputFormat = c .String ("format" )
13084 }
@@ -133,7 +87,7 @@ func imagesCmd(c *cli.Context) error {
13387 if len (c .Args ()) == 1 {
13488 name = c .Args ().Get (0 )
13589 } else if len (c .Args ()) > 1 {
136- return errors .New ("'buildah images' requires at most 1 argument" )
90+ return errors .New ("'kpod images' requires at most 1 argument" )
13791 }
13892
13993 var params * libkpodimage.FilterParams
@@ -154,40 +108,49 @@ func imagesCmd(c *cli.Context) error {
154108 return outputImages (store , imageList , truncate , digests , quiet , outputFormat , noheading )
155109}
156110
157- func outputHeader (truncate , digests bool ) {
111+ func genImagesFormat (quiet , truncate , digests bool ) (format string ) {
112+ if quiet {
113+ return "{{.ID}}"
114+ }
158115 if truncate {
159- fmt . Printf ( "%-20s %-56s " , "IMAGE ID" , "IMAGE NAME" )
116+ format = "table {{ .ID | printf \" %-20.12s \" }} "
160117 } else {
161- fmt . Printf ( "%-64s %-56s " , "IMAGE ID" , "IMAGE NAME" )
118+ format = "table {{ .ID | printf \" %-64s \" }} "
162119 }
120+ format += "{{ .Name | printf \" %-56s\" }} "
163121
164122 if digests {
165- fmt . Printf ( "%-71s " , "DIGEST" )
123+ format += "{{ .Digest | printf \ " %-71s \" }} "
166124 }
167125
168- fmt .Printf ("%-22s %s\n " , "CREATED AT" , "SIZE" )
126+ format += "{{ .CreatedAt | printf \" %-22s\" }} {{.Size}}"
127+ return
169128}
170129
171130func outputImages (store storage.Store , images []storage.Image , truncate , digests , quiet bool , outputFormat string , noheading bool ) error {
172131 imageOutput := []imageOutputParams {}
173132
133+ lastID := ""
174134 for _ , img := range images {
135+ if quiet && lastID == img .ID {
136+ continue // quiet should not show the same ID multiple times
137+ }
175138 createdTime := img .Created
176139
177140 name := ""
178141 if len (img .Names ) > 0 {
179142 name = img .Names [0 ]
180143 }
181144
182- info , digest , size , _ := libkpodimage .InfoAndDigestAndSize (store , img )
145+ info , imageDigest , size , _ := libkpodimage .InfoAndDigestAndSize (store , img )
183146 if info != nil {
184147 createdTime = info .Created
185148 }
186149
187150 params := imageOutputParams {
188151 ID : img .ID ,
189152 Name : name ,
190- Digest : digest ,
153+ Digest : imageDigest ,
191154 CreatedAt : createdTime .Format ("Jan 2, 2006 15:04" ),
192155 Size : libkpodimage .FormattedSize (size ),
193156 }
@@ -196,20 +159,45 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
196159
197160 var out formats.Writer
198161
199- if outputFormat != "" {
200- switch outputFormat {
201- case "json" :
202- out = formats.JSONstruct {Output : toGeneric (imageOutput )}
203- default :
204- // Assuming Go-template
205- out = formats.StdoutTemplate {Output : toGeneric (imageOutput ), Template : outputFormat }
206-
207- }
208- } else {
209- out = stdoutstruct {output : imageOutput , digests : digests , truncate : truncate , quiet : quiet , noheading : noheading }
162+ switch outputFormat {
163+ case "json" :
164+ out = formats.JSONstruct {Output : toGeneric (imageOutput )}
165+ default :
166+ out = formats.StdoutTemplate {Output : toGeneric (imageOutput ), Template : outputFormat , Fields : imageOutput [0 ].headerMap ()}
210167 }
211168
212169 formats .Writer (out ).Out ()
213170
214171 return nil
215172}
173+
174+ type imageOutputParams struct {
175+ ID string `json:"id"`
176+ Name string `json:"names"`
177+ Digest digest.Digest `json:"digest"`
178+ CreatedAt string `json:"created"`
179+ Size string `json:"size"`
180+ }
181+
182+ func toGeneric (params []imageOutputParams ) []interface {} {
183+ genericParams := make ([]interface {}, len (params ))
184+ for i , v := range params {
185+ genericParams [i ] = interface {}(v )
186+ }
187+ return genericParams
188+ }
189+
190+ func (i * imageOutputParams ) headerMap () map [string ]string {
191+ v := reflect .Indirect (reflect .ValueOf (i ))
192+ values := make (map [string ]string )
193+
194+ for i := 0 ; i < v .NumField (); i ++ {
195+ key := v .Type ().Field (i ).Name
196+ value := key
197+ if value == "ID" || value == "Name" {
198+ value = "Image" + value
199+ }
200+ values [key ] = fmt .Sprintf ("%s " , strings .ToUpper (splitCamelCase (value )))
201+ }
202+ return values
203+ }
0 commit comments