2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-04-14 05:43:15 -04:00
|
|
|
"io/ioutil"
|
2016-03-16 15:19:22 -04:00
|
|
|
|
2015-12-18 08:03:41 -05:00
|
|
|
"github.com/docker/docker/api/client/formatter"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2016-04-14 05:43:15 -04:00
|
|
|
"github.com/docker/docker/utils/templates"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2016-04-14 05:43:15 -04:00
|
|
|
type preProcessor struct {
|
|
|
|
opts *types.ContainerListOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size sets the size option when called by a template execution.
|
|
|
|
func (p *preProcessor) Size() bool {
|
|
|
|
p.opts.Size = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdPs outputs a list of Docker containers.
|
|
|
|
//
|
|
|
|
// Usage: docker ps [OPTIONS]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdPs(args ...string) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
psFilterArgs = filters.NewArgs()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
|
|
|
size = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
|
|
|
|
all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
|
2015-11-09 09:37:24 -05:00
|
|
|
noTrunc = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2015-11-05 02:08:00 -05:00
|
|
|
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)")
|
|
|
|
last = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)")
|
2015-07-17 00:03:16 -04:00
|
|
|
format = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
|
2015-03-24 23:57:23 -04:00
|
|
|
flFilter = opts.NewListOpts(nil)
|
|
|
|
)
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
if *last == -1 && *nLatest {
|
|
|
|
*last = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them.
|
|
|
|
// They'll get processed in the daemon/server.
|
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 17:02:06 -05:00
|
|
|
options := types.ContainerListOptions{
|
2015-12-04 11:59:44 -05:00
|
|
|
All: *all,
|
|
|
|
Limit: *last,
|
|
|
|
Size: *size,
|
|
|
|
Filter: psFilterArgs,
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 05:43:15 -04:00
|
|
|
pre := &preProcessor{opts: &options}
|
|
|
|
tmpl, err := templates.Parse(*format)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = tmpl.Execute(ioutil.Discard, pre)
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
containers, err := cli.client.ContainerList(context.Background(), options)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-01 17:23:27 -04:00
|
|
|
f := *format
|
|
|
|
if len(f) == 0 {
|
2015-08-14 18:00:48 -04:00
|
|
|
if len(cli.PsFormat()) > 0 && !*quiet {
|
2015-05-01 17:23:27 -04:00
|
|
|
f = cli.PsFormat()
|
|
|
|
} else {
|
|
|
|
f = "table"
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-01-21 18:52:22 -05:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-18 08:03:41 -05:00
|
|
|
psCtx := formatter.ContainerContext{
|
|
|
|
Context: formatter.Context{
|
|
|
|
Output: cli.out,
|
|
|
|
Format: f,
|
|
|
|
Quiet: *quiet,
|
|
|
|
Trunc: !*noTrunc,
|
|
|
|
},
|
|
|
|
Size: *size,
|
|
|
|
Containers: containers,
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-12-18 08:03:41 -05:00
|
|
|
psCtx.Write()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|