2016-06-08 08:56:44 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2016-09-13 14:53:11 -04:00
|
|
|
"io/ioutil"
|
|
|
|
|
2016-06-08 08:56:44 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-08 08:56:44 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-08 08:56:44 -04:00
|
|
|
"github.com/docker/docker/utils/templates"
|
|
|
|
"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-06-08 08:56:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPsCommand creates a new cobra.Command for `docker ps`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewPsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-13 14:53:11 -04:00
|
|
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
2016-06-08 08:56:44 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ps [OPTIONS]",
|
|
|
|
Short: "List containers",
|
2016-07-29 13:41:52 -04:00
|
|
|
Args: cli.NoArgs,
|
2016-06-08 08:56:44 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runPs(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display numeric IDs")
|
|
|
|
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
|
|
|
|
flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
|
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.BoolVarP(&opts.nLatest, "latest", "l", false, "Show the latest created container (includes all states)")
|
2016-06-22 15:14:16 -04:00
|
|
|
flags.IntVarP(&opts.last, "last", "n", -1, "Show n last created containers (includes all states)")
|
2016-06-08 08:56:44 -04:00
|
|
|
flags.StringVarP(&opts.format, "format", "", "", "Pretty-print containers using a Go template")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-06-08 08:56:44 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-06-23 13:03:40 -04:00
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
cmd := *NewPsCommand(dockerCli)
|
|
|
|
cmd.Aliases = []string{"ps", "list"}
|
|
|
|
cmd.Use = "ls [OPTIONS]"
|
|
|
|
return &cmd
|
|
|
|
}
|
|
|
|
|
2016-11-12 19:44:51 -05:00
|
|
|
// listOptionsProcessor is used to set any container list options which may only
|
|
|
|
// be embedded in the format template.
|
|
|
|
// This is passed directly into tmpl.Execute in order to allow the preprocessor
|
|
|
|
// to set any list options that were not provided by flags (e.g. `.Size`).
|
|
|
|
// It is using a `map[string]bool` so that unknown fields passed into the
|
|
|
|
// template format do not cause errors. These errors will get picked up when
|
|
|
|
// running through the actual template processor.
|
|
|
|
type listOptionsProcessor map[string]bool
|
|
|
|
|
|
|
|
// Size sets the size of the map when called by a template execution.
|
|
|
|
func (o listOptionsProcessor) Size() bool {
|
|
|
|
o["size"] = true
|
2016-07-15 18:37:05 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:17:56 -05:00
|
|
|
// Label is needed here as it allows the correct pre-processing
|
|
|
|
// because Label() is a method with arguments
|
|
|
|
func (o listOptionsProcessor) Label(name string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-07-15 18:37:05 -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-07-15 18:37:05 -04:00
|
|
|
}
|
2016-06-08 08:56:44 -04:00
|
|
|
|
|
|
|
if opts.nLatest && opts.last == -1 {
|
2016-07-15 18:37:05 -04:00
|
|
|
options.Limit = 1
|
2016-06-08 08:56:44 -04:00
|
|
|
}
|
|
|
|
|
2016-07-15 18:37:05 -04:00
|
|
|
tmpl, err := templates.Parse(opts.format)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-08 08:56:44 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 19:44:51 -05:00
|
|
|
optionsProcessor := listOptionsProcessor{}
|
2016-07-15 18:37:05 -04:00
|
|
|
// This shouldn't error out but swallowing the error makes it harder
|
|
|
|
// to track down if preProcessor issues come up. Ref #24696
|
2016-11-12 19:44:51 -05:00
|
|
|
if err := tmpl.Execute(ioutil.Discard, optionsProcessor); err != nil {
|
2016-07-15 18:37:05 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-12 19:44:51 -05:00
|
|
|
// At the moment all we need is to capture .Size for preprocessor
|
|
|
|
options.Size = opts.size || optionsProcessor["size"]
|
2016-06-08 08:56:44 -04:00
|
|
|
|
2016-07-15 18:37:05 -04:00
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runPs(dockerCli *command.DockerCli, opts *psOptions) error {
|
2016-07-15 18:37:05 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
listOptions, err := buildContainerListOptions(opts)
|
2016-06-08 08:56:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-15 18:37:05 -04:00
|
|
|
containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
|
2016-06-08 08:56:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
2016-08-04 09:00:00 -04:00
|
|
|
if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().PsFormat
|
2016-06-08 08:56:44 -04:00
|
|
|
} else {
|
2016-09-13 14:21:07 -04:00
|
|
|
format = formatter.TableFormatKey
|
2016-06-08 08:56:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
containerCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2016-09-13 14:21:07 -04:00
|
|
|
Format: formatter.NewContainerFormat(format, opts.quiet, listOptions.Size),
|
2016-09-12 16:59:18 -04:00
|
|
|
Trunc: !opts.noTrunc,
|
2016-06-08 08:56:44 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.ContainerWrite(containerCtx, containers)
|
2016-06-08 08:56:44 -04:00
|
|
|
}
|