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"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
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`
|
2023-11-20 11:38:50 -05: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")
|
2023-09-09 18:27:44 -04:00
|
|
|
return runPs(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": "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
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func newListCommand(dockerCLI command.Cli) *cobra.Command {
|
|
|
|
cmd := *NewPsCommand(dockerCLI)
|
2016-06-23 13:03:40 -04:00
|
|
|
cmd.Aliases = []string{"ps", "list"}
|
|
|
|
cmd.Use = "ls [OPTIONS]"
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func buildContainerListOptions(options *psOptions) (*container.ListOptions, error) {
|
|
|
|
listOptions := &container.ListOptions{
|
|
|
|
All: options.all,
|
|
|
|
Limit: options.last,
|
|
|
|
Size: options.size,
|
|
|
|
Filters: options.filter.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
if options.nLatest && options.last == -1 {
|
|
|
|
listOptions.Limit = 1
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-03-02 11:05:28 -05:00
|
|
|
// always validate template when `--format` is used, for consistency
|
2023-11-20 11:38:50 -05:00
|
|
|
if len(options.format) > 0 {
|
|
|
|
tmpl, err := templates.NewParse("", options.format)
|
2019-12-24 10:30:23 -05:00
|
|
|
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
|
2023-11-20 11:38:50 -05:00
|
|
|
if !options.quiet && !listOptions.Size && !options.sizeChanged {
|
2023-03-02 11:05:28 -05:00
|
|
|
// 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 {
|
2023-11-20 11:38:50 -05:00
|
|
|
listOptions.Size = true
|
2023-03-02 11:05:28 -05:00
|
|
|
}
|
2019-12-24 10:30:23 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
return listOptions, nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPs(ctx context.Context, dockerCLI command.Cli, options *psOptions) error {
|
2022-05-30 21:33:19 -04:00
|
|
|
if len(options.format) == 0 {
|
|
|
|
// load custom psFormat from CLI config (if any)
|
2023-11-20 11:38:50 -05:00
|
|
|
options.format = dockerCLI.ConfigFile().PsFormat
|
2023-04-09 19:14:21 -04:00
|
|
|
} else if options.quiet {
|
2023-11-20 11:38:50 -05:00
|
|
|
_, _ = dockerCLI.Err().Write([]byte("WARNING: Ignoring custom format, because both --format and --quiet are set.\n"))
|
2022-05-30 21:33:19 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
containers, err := dockerCLI.Client().ContainerList(ctx, *listOptions)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
containerCtx := formatter.Context{
|
2023-11-20 11:38:50 -05:00
|
|
|
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
|
|
|
}
|