2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:05:59 -05:00
|
|
|
"io"
|
2016-09-13 14:53:11 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"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"
|
2017-08-08 11:26:24 -04:00
|
|
|
"github.com/docker/cli/templates"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2019-12-24 10:30:23 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type psOptions struct {
|
2023-03-02 11:05:28 -05:00
|
|
|
quiet bool
|
|
|
|
size bool
|
|
|
|
sizeChanged bool
|
|
|
|
all bool
|
|
|
|
noTrunc bool
|
|
|
|
nLatest bool
|
|
|
|
last int
|
|
|
|
format string
|
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPsCommand creates a new cobra.Command for `docker ps`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewPsCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := psOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ps [OPTIONS]",
|
|
|
|
Short: "List containers",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-03-02 11:05:28 -05:00
|
|
|
options.sizeChanged = cmd.Flags().Changed("size")
|
2017-05-15 08:45:19 -04:00
|
|
|
return runPs(dockerCli, &options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 03:37:08 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"category-top": "3",
|
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 container ls, docker container list, docker container ps, docker ps",
|
2022-03-30 03:37:08 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
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 display container IDs")
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.size, "size", "s", false, "Display total file sizes")
|
|
|
|
flags.BoolVarP(&options.all, "all", "a", false, "Show all containers (default shows just running)")
|
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.BoolVarP(&options.nLatest, "latest", "l", false, "Show the latest created container (includes all states)")
|
|
|
|
flags.IntVarP(&options.last, "last", "n", -1, "Show n last created containers (includes all states)")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVarP(&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-10-11 12:18:27 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2016-06-23 13:03:40 -04:00
|
|
|
cmd := *NewPsCommand(dockerCli)
|
|
|
|
cmd.Aliases = []string{"ps", "list"}
|
|
|
|
cmd.Use = "ls [OPTIONS]"
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
|
|
|
|
options := &types.ContainerListOptions{
|
2016-11-01 10:01:16 -04:00
|
|
|
All: opts.all,
|
|
|
|
Limit: opts.last,
|
|
|
|
Size: opts.size,
|
|
|
|
Filters: opts.filter.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.nLatest && opts.last == -1 {
|
|
|
|
options.Limit = 1
|
|
|
|
}
|
|
|
|
|
2023-03-02 11:05:28 -05:00
|
|
|
// always validate template when `--format` is used, for consistency
|
|
|
|
if len(opts.format) > 0 {
|
2019-12-24 10:30:23 -05:00
|
|
|
tmpl, err := templates.NewParse("", opts.format)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse template")
|
|
|
|
}
|
|
|
|
|
|
|
|
optionsProcessor := formatter.NewContainerContext()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// This shouldn't error out but swallowing the error makes it harder
|
|
|
|
// to track down if preProcessor issues come up.
|
2022-02-25 07:05:59 -05:00
|
|
|
if err := tmpl.Execute(io.Discard, optionsProcessor); err != nil {
|
2019-12-24 10:30:23 -05:00
|
|
|
return nil, errors.Wrap(err, "failed to execute template")
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2023-03-02 11:05:28 -05:00
|
|
|
// if `size` was not explicitly set to false (with `--size=false`)
|
|
|
|
// and `--quiet` is not set, request size if the template requires it
|
|
|
|
if !opts.quiet && !options.Size && !opts.sizeChanged {
|
|
|
|
// The --size option isn't set, but .Size may be used in the template.
|
|
|
|
// Parse and execute the given template to detect if the .Size field is
|
|
|
|
// used. If it is, then automatically enable the --size option. See #24696
|
|
|
|
//
|
|
|
|
// Only requesting container size information when needed is an optimization,
|
|
|
|
// because calculating the size is a costly operation.
|
|
|
|
|
|
|
|
if _, ok := optionsProcessor.FieldsUsed["Size"]; ok {
|
|
|
|
options.Size = true
|
|
|
|
}
|
2019-12-24 10:30:23 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runPs(dockerCli command.Cli, options *psOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-05-30 21:33:19 -04:00
|
|
|
if len(options.format) == 0 {
|
|
|
|
// load custom psFormat from CLI config (if any)
|
|
|
|
options.format = dockerCli.ConfigFile().PsFormat
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
listOptions, err := buildContainerListOptions(options)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
containerCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2022-05-30 21:33:19 -04:00
|
|
|
Format: formatter.NewContainerFormat(options.format, options.quiet, listOptions.Size),
|
2017-05-15 08:45:19 -04:00
|
|
|
Trunc: !options.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.ContainerWrite(containerCtx, containers)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|