2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2024-03-28 12:49:04 -04:00
|
|
|
"errors"
|
2024-02-03 11:57:48 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-05-03 21:02:44 -04:00
|
|
|
|
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"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2024-01-24 08:32:07 -05:00
|
|
|
"github.com/docker/docker/api/types/image"
|
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
|
2024-02-03 11:57:48 -05:00
|
|
|
calledAs string
|
2024-03-28 12:49:04 -04:00
|
|
|
tree bool
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewImagesCommand creates a new `docker images` command
|
2024-02-03 11:56:06 -05:00
|
|
|
func NewImagesCommand(dockerCLI command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := 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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.matchName = args[0]
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2024-02-03 11:57:48 -05:00
|
|
|
// Pass through how the command was invoked. We use this to print
|
|
|
|
// warnings when an ambiguous argument was passed when using the
|
|
|
|
// legacy (top-level) "docker images" subcommand.
|
|
|
|
options.calledAs = cmd.CalledAs()
|
2024-02-03 11:56:06 -05:00
|
|
|
return runImages(cmd.Context(), dockerCLI, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 03:37:08 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"category-top": "7",
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
"aliases": "docker image ls, docker image list, docker images",
|
2022-03-30 03:37:08 -04:00
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2020-03-02 04:28:52 -05:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only show image IDs")
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.all, "all", "a", false, "Show all images (default hides intermediate images)")
|
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.BoolVar(&options.showDigests, "digests", false, "Show digests")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2024-03-28 12:49:04 -04:00
|
|
|
flags.BoolVar(&options.tree, "tree", false, "List multi-platform images as a tree (EXPERIMENTAL)")
|
|
|
|
flags.SetAnnotation("tree", "version", []string{"1.47"})
|
|
|
|
flags.SetAnnotation("tree", "experimentalCLI", nil)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:56:06 -05:00
|
|
|
func newListCommand(dockerCLI command.Cli) *cobra.Command {
|
|
|
|
cmd := *NewImagesCommand(dockerCLI)
|
2020-06-14 15:35:17 -04:00
|
|
|
cmd.Aliases = []string{"list"}
|
2016-06-23 13:03:40 -04:00
|
|
|
cmd.Use = "ls [OPTIONS] [REPOSITORY[:TAG]]"
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:56:06 -05:00
|
|
|
func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
filters := options.filter.Value()
|
|
|
|
if options.matchName != "" {
|
|
|
|
filters.Add("reference", options.matchName)
|
2016-11-11 09:34:01 -05:00
|
|
|
}
|
|
|
|
|
2024-03-28 12:49:04 -04:00
|
|
|
if options.tree {
|
|
|
|
if options.quiet {
|
|
|
|
return errors.New("--quiet is not yet supported with --tree")
|
|
|
|
}
|
|
|
|
if options.noTrunc {
|
|
|
|
return errors.New("--no-trunc is not yet supported with --tree")
|
|
|
|
}
|
|
|
|
if options.showDigests {
|
|
|
|
return errors.New("--show-digest is not yet supported with --tree")
|
|
|
|
}
|
|
|
|
if options.format != "" {
|
|
|
|
return errors.New("--format is not yet supported with --tree")
|
|
|
|
}
|
|
|
|
|
|
|
|
return runTree(ctx, dockerCLI, treeOptions{
|
|
|
|
all: options.all,
|
|
|
|
filters: filters,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:56:06 -05:00
|
|
|
images, err := dockerCLI.Client().ImageList(ctx, image.ListOptions{
|
2017-05-15 08:45:19 -04:00
|
|
|
All: options.all,
|
2016-11-11 09:34:01 -05:00
|
|
|
Filters: filters,
|
2024-02-03 11:54:23 -05:00
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-09-12 16:59:18 -04:00
|
|
|
if len(format) == 0 {
|
2024-02-03 11:56:06 -05:00
|
|
|
if len(dockerCLI.ConfigFile().ImagesFormat) > 0 && !options.quiet {
|
|
|
|
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{
|
2024-02-03 11:56:06 -05:00
|
|
|
Output: dockerCLI.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewImageFormat(format, options.quiet, options.showDigests),
|
|
|
|
Trunc: !options.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-15 08:45:19 -04:00
|
|
|
Digest: options.showDigests,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2024-02-03 11:57:48 -05:00
|
|
|
if err := formatter.ImageWrite(imageCtx, images); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if options.matchName != "" && len(images) == 0 && options.calledAs == "images" {
|
|
|
|
printAmbiguousHint(dockerCLI.Err(), options.matchName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printAmbiguousHint prints an informational warning if the provided filter
|
|
|
|
// argument is ambiguous.
|
|
|
|
//
|
|
|
|
// The "docker images" top-level subcommand predates the "docker <object> <verb>"
|
|
|
|
// convention (e.g. "docker image ls"), but accepts a positional argument to
|
|
|
|
// search/filter images by name (globbing). It's common for users to accidentally
|
|
|
|
// mistake these commands, and to use (e.g.) "docker images ls", expecting
|
|
|
|
// to see all images, but ending up with an empty list because no image named
|
|
|
|
// "ls" was found.
|
|
|
|
//
|
|
|
|
// Disallowing these search-terms would be a breaking change, but we can print
|
|
|
|
// and informational message to help the users correct their mistake.
|
|
|
|
func printAmbiguousHint(stdErr io.Writer, matchName string) {
|
|
|
|
switch matchName {
|
|
|
|
// List of subcommands for "docker image" and their aliases (see "docker image --help"):
|
|
|
|
case "build",
|
|
|
|
"history",
|
|
|
|
"import",
|
|
|
|
"inspect",
|
|
|
|
"list",
|
|
|
|
"load",
|
|
|
|
"ls",
|
|
|
|
"prune",
|
|
|
|
"pull",
|
|
|
|
"push",
|
|
|
|
"rm",
|
|
|
|
"save",
|
|
|
|
"tag":
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(stdErr, "\nNo images found matching %q: did you mean \"docker image %[1]s\"?\n", matchName)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|