2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-03 20:39:06 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2015-05-01 17:23:27 -04:00
|
|
|
"github.com/docker/docker/api/client/ps"
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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"
|
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
psFilterArgs = filters.Args{}
|
|
|
|
v = url.Values{}
|
|
|
|
|
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)")
|
|
|
|
noTrunc = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
|
|
|
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container, include non-running")
|
|
|
|
since = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show created since Id or Name, include non-running")
|
|
|
|
before = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name")
|
|
|
|
last = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if *all {
|
|
|
|
v.Set("all", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *last != -1 {
|
|
|
|
v.Set("limit", strconv.Itoa(*last))
|
|
|
|
}
|
|
|
|
|
|
|
|
if *since != "" {
|
|
|
|
v.Set("since", *since)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *before != "" {
|
|
|
|
v.Set("before", *before)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *size {
|
|
|
|
v.Set("size", "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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(psFilterArgs) > 0 {
|
2015-03-25 22:31:29 -04:00
|
|
|
filterJSON, err := filters.ToParam(psFilterArgs)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:31:29 -04:00
|
|
|
v.Set("filters", filterJSON)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("GET", "/containers/json?"+v.Encode(), nil, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
containers := []types.Container{}
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&containers); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
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-05-01 17:23:27 -04:00
|
|
|
psCtx := ps.Context{
|
|
|
|
Output: cli.out,
|
|
|
|
Format: f,
|
|
|
|
Quiet: *quiet,
|
|
|
|
Size: *size,
|
|
|
|
Trunc: !*noTrunc,
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-01 17:23:27 -04:00
|
|
|
ps.Format(psCtx, containers)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|