2017-06-16 17:24:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/containers/storage"
|
2017-07-24 01:38:03 +00:00
|
|
|
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
|
2017-07-25 19:33:41 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-06-16 17:24:00 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type imageOutputParams struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
2017-07-25 19:33:41 +00:00
|
|
|
Digest digest.Digest
|
2017-06-16 17:24:00 +00:00
|
|
|
CreatedAt string
|
|
|
|
Size string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
imagesFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet, q",
|
|
|
|
Usage: "display only image IDs",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "noheading, n",
|
|
|
|
Usage: "do not print column headings",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-trunc, notruncate",
|
|
|
|
Usage: "do not truncate output",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "digests",
|
|
|
|
Usage: "show digests",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Usage: "pretty-print images using a Go template. will override --quiet",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "filter, f",
|
|
|
|
Usage: "filter output based on conditions provided (default [])",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
imagesDescription = "lists locally stored images."
|
|
|
|
imagesCommand = cli.Command{
|
|
|
|
Name: "images",
|
|
|
|
Usage: "list images in local storage",
|
|
|
|
Description: imagesDescription,
|
|
|
|
Flags: imagesFlags,
|
|
|
|
Action: imagesCmd,
|
|
|
|
ArgsUsage: "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func imagesCmd(c *cli.Context) error {
|
2017-07-27 17:18:07 +00:00
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Could not get config")
|
|
|
|
}
|
|
|
|
store, err := getStore(config)
|
2017-06-16 17:24:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
quiet := false
|
|
|
|
if c.IsSet("quiet") {
|
|
|
|
quiet = c.Bool("quiet")
|
|
|
|
}
|
|
|
|
noheading := false
|
|
|
|
if c.IsSet("noheading") {
|
|
|
|
noheading = c.Bool("noheading")
|
|
|
|
}
|
|
|
|
truncate := true
|
|
|
|
if c.IsSet("no-trunc") {
|
|
|
|
truncate = !c.Bool("no-trunc")
|
|
|
|
}
|
|
|
|
digests := false
|
|
|
|
if c.IsSet("digests") {
|
|
|
|
digests = c.Bool("digests")
|
|
|
|
}
|
|
|
|
formatString := ""
|
|
|
|
hasTemplate := false
|
|
|
|
if c.IsSet("format") {
|
|
|
|
formatString = c.String("format")
|
|
|
|
hasTemplate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
name := ""
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
var params *libkpodimage.FilterParams
|
2017-06-16 17:24:00 +00:00
|
|
|
if c.IsSet("filter") {
|
2017-07-24 01:38:03 +00:00
|
|
|
params, err = libkpodimage.ParseFilter(store, c.String("filter"))
|
2017-06-16 17:24:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing filter")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
params = nil
|
|
|
|
}
|
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
imageList, err := libkpodimage.GetImagesMatchingFilter(store, params, name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get list of images matching filter")
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-24 01:38:03 +00:00
|
|
|
if len(imageList) > 0 && !noheading && !quiet && !hasTemplate {
|
|
|
|
outputHeader(truncate, digests)
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
return outputImages(store, imageList, formatString, hasTemplate, truncate, digests, quiet)
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func outputHeader(truncate, digests bool) {
|
|
|
|
if truncate {
|
|
|
|
fmt.Printf("%-20s %-56s ", "IMAGE ID", "IMAGE NAME")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%-64s %-56s ", "IMAGE ID", "IMAGE NAME")
|
|
|
|
}
|
|
|
|
|
|
|
|
if digests {
|
2017-07-19 03:07:29 +00:00
|
|
|
fmt.Printf("%-71s ", "DIGEST")
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%-22s %s\n", "CREATED AT", "SIZE")
|
|
|
|
}
|
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
func outputImages(store storage.Store, images []storage.Image, format string, hasTemplate, truncate, digests, quiet bool) error {
|
2017-07-21 20:43:30 +00:00
|
|
|
for _, img := range images {
|
2017-07-25 19:33:41 +00:00
|
|
|
createdTime := img.Created.Format("Jan 2, 2006 15:04")
|
|
|
|
|
|
|
|
name := ""
|
|
|
|
if len(img.Names) > 0 {
|
|
|
|
name = img.Names[0]
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-25 19:33:41 +00:00
|
|
|
|
|
|
|
digest, size, _ := libkpodimage.DigestAndSize(store, img)
|
2017-06-16 17:24:00 +00:00
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
if quiet {
|
|
|
|
fmt.Printf("%-64s\n", img.ID)
|
|
|
|
// We only want to print each id once
|
|
|
|
break
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 01:38:03 +00:00
|
|
|
params := imageOutputParams{
|
|
|
|
ID: img.ID,
|
2017-07-25 19:33:41 +00:00
|
|
|
Name: name,
|
2017-07-24 01:38:03 +00:00
|
|
|
Digest: digest,
|
|
|
|
CreatedAt: createdTime,
|
|
|
|
Size: libkpodimage.FormattedSize(size),
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-24 01:38:03 +00:00
|
|
|
if hasTemplate {
|
2017-07-25 19:33:41 +00:00
|
|
|
if err := outputUsingTemplate(format, params); err != nil {
|
2017-07-24 01:38:03 +00:00
|
|
|
return err
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-24 01:38:03 +00:00
|
|
|
continue
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-24 01:38:03 +00:00
|
|
|
outputUsingFormatString(truncate, digests, params)
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
2017-07-24 01:38:03 +00:00
|
|
|
return nil
|
2017-06-16 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func outputUsingTemplate(format string, params imageOutputParams) error {
|
|
|
|
tmpl, err := template.New("image").Parse(format)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Template parsing error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(os.Stdout, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputUsingFormatString(truncate, digests bool, params imageOutputParams) {
|
|
|
|
if truncate {
|
|
|
|
fmt.Printf("%-20.12s %-56s", params.ID, params.Name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%-64s %-56s", params.ID, params.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if digests {
|
|
|
|
fmt.Printf(" %-64s", params.Digest)
|
|
|
|
}
|
|
|
|
fmt.Printf(" %-22s %s\n", params.CreatedAt, params.Size)
|
|
|
|
}
|