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"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
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 {
|
|
|
|
quiet bool
|
|
|
|
size bool
|
|
|
|
all bool
|
|
|
|
noTrunc bool
|
|
|
|
nLatest bool
|
|
|
|
last int
|
|
|
|
format string
|
2016-09-13 14:53:11 -04:00
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runPs(dockerCli, &options)
|
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)")
|
|
|
|
flags.StringVarP(&options.format, "format", "", "", "Pretty-print containers using a Go template")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
options.Size = opts.size
|
|
|
|
if !options.Size && len(opts.format) > 0 {
|
|
|
|
// 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.
|
|
|
|
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
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
if _, ok := optionsProcessor.FieldsUsed["Size"]; ok {
|
|
|
|
options.Size = true
|
|
|
|
}
|
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()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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().PsFormat) > 0 && !options.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().PsFormat
|
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
|
|
|
containerCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewContainerFormat(format, options.quiet, listOptions.Size),
|
|
|
|
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
|
|
|
}
|