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:])
headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template)
if err != nil {
errors.Wrapf(err, "Template parsing error")
return errors.Wrapf(err, "Template parsing error")
}
err = headerTmpl.Execute(os.Stdout, t.Fields)
if err != nil {
return err
}
fmt.Println()
}
tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template)

View file

@ -14,7 +14,7 @@ var basicFunctions = template.FuncMap{
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.Encode(v)
_ = enc.Encode(v)
// Remove the trailing new line added by the encoder
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 {
config, err := getConfig(c)
if err != nil {
@ -119,7 +78,7 @@ func imagesCmd(c *cli.Context) error {
if c.IsSet("digests") {
digests = c.Bool("digests")
}
outputFormat := ""
outputFormat := genImagesFormat(quiet, truncate, digests)
if c.IsSet("format") {
outputFormat = c.String("format")
}
@ -128,7 +87,7 @@ func imagesCmd(c *cli.Context) error {
if len(c.Args()) == 1 {
name = c.Args().Get(0)
} 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
@ -149,24 +108,33 @@ func imagesCmd(c *cli.Context) error {
return outputImages(store, imageList, truncate, digests, quiet, outputFormat, noheading)
}
func outputHeader(truncate, digests bool) {
if truncate {
fmt.Printf("%-20s %-56s ", "IMAGE ID", "IMAGE NAME")
} else {
fmt.Printf("%-64s %-56s ", "IMAGE ID", "IMAGE NAME")
func genImagesFormat(quiet, truncate, digests bool) (format string) {
if quiet {
return "{{.ID}}"
}
if truncate {
format = "table {{ .ID | printf \"%-20.12s\" }} "
} else {
format = "table {{ .ID | printf \"%-64s\" }} "
}
format += "{{ .Name | printf \"%-56s\" }} "
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 {
imageOutput := []imageOutputParams{}
lastID := ""
for _, img := range images {
if quiet && lastID == img.ID {
continue // quiet should not show the same ID multiple times
}
createdTime := img.Created
name := ""
@ -174,7 +142,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
name = img.Names[0]
}
info, digest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img)
info, imageDigest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img)
if info != nil {
createdTime = info.Created
}
@ -182,7 +150,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
params := imageOutputParams{
ID: img.ID,
Name: name,
Digest: digest,
Digest: imageDigest,
CreatedAt: createdTime.Format("Jan 2, 2006 15:04"),
Size: libkpodimage.FormattedSize(size),
}
@ -191,15 +159,11 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
var out formats.Writer
if outputFormat != "" {
switch outputFormat {
case "json":
out = formats.JSONstruct{Output: toGeneric(imageOutput)}
default:
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}
switch outputFormat {
case "json":
out = formats.JSONstruct{Output: toGeneric(imageOutput)}
default:
out = formats.StdoutTemplate{Output: toGeneric(imageOutput), Template: outputFormat, Fields: imageOutput[0].headerMap()}
}
formats.Writer(out).Out()
@ -215,6 +179,14 @@ type imageOutputParams struct {
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 {
v := reflect.Indirect(reflect.ValueOf(i))
values := make(map[string]string)