2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"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"
|
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"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"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
|
2017-03-30 20:21:14 -04: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
|
|
|
}
|
2023-09-09 18:27:44 -04: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
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2016-06-23 13:03:40 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04: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
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
listOptions := types.ImageListOptions{
|
|
|
|
All: options.all,
|
2016-11-11 09:34:01 -05:00
|
|
|
Filters: filters,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
images, err := dockerCli.Client().ImageList(ctx, listOptions)
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().ImagesFormat) > 0 && !options.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(),
|
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
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.ImageWrite(imageCtx, images)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|