2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type imagesOptions struct {
|
|
|
|
matchName string
|
|
|
|
|
|
|
|
quiet bool
|
|
|
|
all bool
|
|
|
|
noTrunc bool
|
|
|
|
showDigests bool
|
|
|
|
format string
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewImagesCommand creates a new `docker images` command
|
|
|
|
func NewImagesCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-13 14:53:11 -04:00
|
|
|
opts := imagesOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "images [OPTIONS] [REPOSITORY[:TAG]]",
|
|
|
|
Short: "List images",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
opts.matchName = args[0]
|
|
|
|
}
|
|
|
|
return runImages(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show numeric IDs")
|
|
|
|
flags.BoolVarP(&opts.all, "all", "a", false, "Show all images (default hides intermediate images)")
|
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.BoolVar(&opts.showDigests, "digests", false, "Show digests")
|
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-06-23 13:03:40 -04:00
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
cmd := *NewImagesCommand(dockerCli)
|
|
|
|
cmd.Aliases = []string{"images", "list"}
|
|
|
|
cmd.Use = "ls [OPTIONS] [REPOSITORY[:TAG]]"
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runImages(dockerCli *command.DockerCli, opts imagesOptions) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-11-11 09:34:01 -05:00
|
|
|
filters := opts.filter.Value()
|
|
|
|
if opts.matchName != "" {
|
|
|
|
filters.Add("reference", opts.matchName)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
options := types.ImageListOptions{
|
2016-11-11 09:34:01 -05:00
|
|
|
All: opts.all,
|
|
|
|
Filters: filters,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
images, err := dockerCli.Client().ImageList(ctx, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
2016-09-08 13:11:39 -04:00
|
|
|
if len(dockerCli.ConfigFile().ImagesFormat) > 0 && !opts.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().ImagesFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2016-09-13 14:21:07 -04:00
|
|
|
format = formatter.TableFormatKey
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
imageCtx := formatter.ImageContext{
|
2016-09-08 13:11:39 -04:00
|
|
|
Context: formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: formatter.NewImageFormat(format, opts.quiet, opts.showDigests),
|
2016-09-08 13:11:39 -04:00
|
|
|
Trunc: !opts.noTrunc,
|
|
|
|
},
|
|
|
|
Digest: opts.showDigests,
|
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.ImageWrite(imageCtx, images)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|