Make kpod images use text/template by default

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-08-15 22:23:40 -04:00
parent 08c3d241a4
commit ba07bfb932
3 changed files with 38 additions and 63 deletions

View file

@ -43,9 +43,12 @@ func (t StdoutTemplate) Out() error {
t.Template = strings.TrimSpace(t.Template[5:]) t.Template = strings.TrimSpace(t.Template[5:])
headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template) headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template)
if err != nil { if err != nil {
errors.Wrapf(err, "Template parsing error") return errors.Wrapf(err, "Template parsing error")
} }
err = headerTmpl.Execute(os.Stdout, t.Fields) err = headerTmpl.Execute(os.Stdout, t.Fields)
if err != nil {
return err
}
fmt.Println() fmt.Println()
} }
tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template) tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template)

View file

@ -14,7 +14,7 @@ var basicFunctions = template.FuncMap{
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
enc := json.NewEncoder(buf) enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false) enc.SetEscapeHTML(false)
enc.Encode(v) _ = enc.Encode(v)
// Remove the trailing new line added by the encoder // Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String()) return strings.TrimSpace(buf.String())
}, },

View file

@ -52,47 +52,6 @@ var (
} }
) )
type stdoutstruct struct {
output []imageOutputParams
truncate, digests, quiet, noheading bool
}
func (so stdoutstruct) Out() error {
if len(so.output) > 0 && !so.noheading && !so.quiet {
outputHeader(so.truncate, so.digests)
}
lastID := ""
for _, img := range so.output {
if so.quiet {
if lastID == img.ID {
continue // quiet should not show the same ID multiple times.
}
fmt.Printf("%-64s\n", img.ID)
continue
}
if so.truncate {
fmt.Printf("%-20.12s %-56s", img.ID, img.Name)
} else {
fmt.Printf("%-64s %-56s", img.ID, img.Name)
}
if so.digests {
fmt.Printf(" %-64s", img.Digest)
}
fmt.Printf(" %-22s %s\n", img.CreatedAt, img.Size)
}
return nil
}
func toGeneric(params []imageOutputParams) []interface{} {
genericParams := make([]interface{}, len(params))
for i, v := range params {
genericParams[i] = interface{}(v)
}
return genericParams
}
func imagesCmd(c *cli.Context) error { func imagesCmd(c *cli.Context) error {
config, err := getConfig(c) config, err := getConfig(c)
if err != nil { if err != nil {
@ -119,7 +78,7 @@ func imagesCmd(c *cli.Context) error {
if c.IsSet("digests") { if c.IsSet("digests") {
digests = c.Bool("digests") digests = c.Bool("digests")
} }
outputFormat := "" outputFormat := genImagesFormat(quiet, truncate, digests)
if c.IsSet("format") { if c.IsSet("format") {
outputFormat = c.String("format") outputFormat = c.String("format")
} }
@ -128,7 +87,7 @@ func imagesCmd(c *cli.Context) error {
if len(c.Args()) == 1 { if len(c.Args()) == 1 {
name = c.Args().Get(0) name = c.Args().Get(0)
} else if len(c.Args()) > 1 { } else if len(c.Args()) > 1 {
return errors.New("'buildah images' requires at most 1 argument") return errors.New("'kpod images' requires at most 1 argument")
} }
var params *libkpodimage.FilterParams var params *libkpodimage.FilterParams
@ -149,24 +108,33 @@ func imagesCmd(c *cli.Context) error {
return outputImages(store, imageList, truncate, digests, quiet, outputFormat, noheading) return outputImages(store, imageList, truncate, digests, quiet, outputFormat, noheading)
} }
func outputHeader(truncate, digests bool) { func genImagesFormat(quiet, truncate, digests bool) (format string) {
if truncate { if quiet {
fmt.Printf("%-20s %-56s ", "IMAGE ID", "IMAGE NAME") return "{{.ID}}"
} else {
fmt.Printf("%-64s %-56s ", "IMAGE ID", "IMAGE NAME")
} }
if truncate {
format = "table {{ .ID | printf \"%-20.12s\" }} "
} else {
format = "table {{ .ID | printf \"%-64s\" }} "
}
format += "{{ .Name | printf \"%-56s\" }} "
if digests { if digests {
fmt.Printf("%-71s ", "DIGEST") format += "{{ .Digest | printf \"%-71s \"}} "
} }
fmt.Printf("%-22s %s\n", "CREATED AT", "SIZE") format += "{{ .CreatedAt | printf \"%-22s\" }} {{.Size}}"
return
} }
func outputImages(store storage.Store, images []storage.Image, truncate, digests, quiet bool, outputFormat string, noheading bool) error { func outputImages(store storage.Store, images []storage.Image, truncate, digests, quiet bool, outputFormat string, noheading bool) error {
imageOutput := []imageOutputParams{} imageOutput := []imageOutputParams{}
lastID := ""
for _, img := range images { for _, img := range images {
if quiet && lastID == img.ID {
continue // quiet should not show the same ID multiple times
}
createdTime := img.Created createdTime := img.Created
name := "" name := ""
@ -174,7 +142,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
name = img.Names[0] name = img.Names[0]
} }
info, digest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img) info, imageDigest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img)
if info != nil { if info != nil {
createdTime = info.Created createdTime = info.Created
} }
@ -182,7 +150,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
params := imageOutputParams{ params := imageOutputParams{
ID: img.ID, ID: img.ID,
Name: name, Name: name,
Digest: digest, Digest: imageDigest,
CreatedAt: createdTime.Format("Jan 2, 2006 15:04"), CreatedAt: createdTime.Format("Jan 2, 2006 15:04"),
Size: libkpodimage.FormattedSize(size), Size: libkpodimage.FormattedSize(size),
} }
@ -191,16 +159,12 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
var out formats.Writer var out formats.Writer
if outputFormat != "" {
switch outputFormat { switch outputFormat {
case "json": case "json":
out = formats.JSONstruct{Output: toGeneric(imageOutput)} out = formats.JSONstruct{Output: toGeneric(imageOutput)}
default: default:
out = formats.StdoutTemplate{Output: toGeneric(imageOutput), Template: outputFormat, Fields: imageOutput[0].headerMap()} out = formats.StdoutTemplate{Output: toGeneric(imageOutput), Template: outputFormat, Fields: imageOutput[0].headerMap()}
} }
} else {
out = stdoutstruct{output: imageOutput, digests: digests, truncate: truncate, quiet: quiet, noheading: noheading}
}
formats.Writer(out).Out() formats.Writer(out).Out()
@ -215,6 +179,14 @@ type imageOutputParams struct {
Size string `json:"size"` Size string `json:"size"`
} }
func toGeneric(params []imageOutputParams) []interface{} {
genericParams := make([]interface{}, len(params))
for i, v := range params {
genericParams[i] = interface{}(v)
}
return genericParams
}
func (i *imageOutputParams) headerMap() map[string]string { func (i *imageOutputParams) headerMap() map[string]string {
v := reflect.Indirect(reflect.ValueOf(i)) v := reflect.Indirect(reflect.ValueOf(i))
values := make(map[string]string) values := make(map[string]string)