summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTianyi Wang <capkurmagati@gmail.com>2016-06-08 21:56:44 +0900
committerTianyi Wang <capkurmagati@gmail.com>2016-06-14 11:59:37 +0900
commit5a0b53b5a3d779e26a51230dfbdbf53bdd6b18d4 (patch)
tree2632828d69d678e672df50b49eda28516edf0d64
parentbfed05be0b9bfc04bf922d79dd8dc420e1e579e2 (diff)
downloaddocker-5a0b53b5a3d779e26a51230dfbdbf53bdd6b18d4.tar.gz
Migrate ps command to cobra
Signed-off-by: Tianyi Wang <capkurmagati@gmail.com>
-rw-r--r--api/client/commands.go1
-rw-r--r--api/client/container/ps.go125
-rw-r--r--api/client/ps.go105
-rw-r--r--cli/cobraadaptor/adaptor.go1
-rw-r--r--cli/usage.go1
5 files changed, 126 insertions, 107 deletions
diff --git a/api/client/commands.go b/api/client/commands.go
index b6ddd5c22e..98c7ba4256 100644
--- a/api/client/commands.go
+++ b/api/client/commands.go
@@ -7,7 +7,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
"exec": cli.CmdExec,
"info": cli.CmdInfo,
"inspect": cli.CmdInspect,
- "ps": cli.CmdPs,
"update": cli.CmdUpdate,
}[name]
}
diff --git a/api/client/container/ps.go b/api/client/container/ps.go
new file mode 100644
index 0000000000..3693ae75b9
--- /dev/null
+++ b/api/client/container/ps.go
@@ -0,0 +1,125 @@
+package container
+
+import (
+ "golang.org/x/net/context"
+
+ "github.com/docker/docker/api/client"
+ "github.com/docker/docker/api/client/formatter"
+ "github.com/docker/docker/cli"
+ "github.com/docker/engine-api/types"
+ "github.com/docker/engine-api/types/filters"
+
+ "github.com/docker/docker/utils/templates"
+ "github.com/spf13/cobra"
+ "io/ioutil"
+)
+
+type psOptions struct {
+ quiet bool
+ size bool
+ all bool
+ noTrunc bool
+ nLatest bool
+ last int
+ format string
+ filter []string
+}
+
+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
+}
+
+// NewPsCommand creates a new cobra.Command for `docker ps`
+func NewPsCommand(dockerCli *client.DockerCli) *cobra.Command {
+ var opts psOptions
+
+ cmd := &cobra.Command{
+ Use: "ps [OPTIONS]",
+ Short: "List containers",
+ Args: cli.ExactArgs(0),
+ 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)")
+ flags.IntVarP(&opts.last, "", "n", -1, "Show n last created containers (includes all states)")
+ flags.StringVarP(&opts.format, "format", "", "", "Pretty-print containers using a Go template")
+ flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Filter output based on conditions provided")
+
+ return cmd
+}
+
+func runPs(dockerCli *client.DockerCli, opts *psOptions) error {
+ ctx := context.Background()
+
+ if opts.nLatest && opts.last == -1 {
+ opts.last = 1
+ }
+
+ containerFilterArgs := filters.NewArgs()
+ for _, f := range opts.filter {
+ var err error
+ containerFilterArgs, err = filters.ParseFlag(f, containerFilterArgs)
+ if err != nil {
+ return err
+ }
+ }
+
+ options := types.ContainerListOptions{
+ All: opts.all,
+ Limit: opts.last,
+ Size: opts.size,
+ Filter: containerFilterArgs,
+ }
+
+ pre := &preProcessor{opts: &options}
+ tmpl, err := templates.Parse(opts.format)
+
+ if err != nil {
+ return err
+ }
+
+ _ = tmpl.Execute(ioutil.Discard, pre)
+
+ containers, err := dockerCli.Client().ContainerList(ctx, options)
+ if err != nil {
+ return err
+ }
+
+ f := opts.format
+ if len(f) == 0 {
+ if len(dockerCli.PsFormat()) > 0 && !opts.quiet {
+ f = dockerCli.PsFormat()
+ } else {
+ f = "table"
+ }
+ }
+
+ psCtx := formatter.ContainerContext{
+ Context: formatter.Context{
+ Output: dockerCli.Out(),
+ Format: f,
+ Quiet: opts.quiet,
+ Trunc: !opts.noTrunc,
+ },
+ Size: opts.size,
+ Containers: containers,
+ }
+
+ psCtx.Write()
+
+ return nil
+}
diff --git a/api/client/ps.go b/api/client/ps.go
deleted file mode 100644
index 0cab9b519f..0000000000
--- a/api/client/ps.go
+++ /dev/null
@@ -1,105 +0,0 @@
-package client
-
-import (
- "golang.org/x/net/context"
- "io/ioutil"
-
- "github.com/docker/docker/api/client/formatter"
- Cli "github.com/docker/docker/cli"
- "github.com/docker/docker/opts"
- flag "github.com/docker/docker/pkg/mflag"
- "github.com/docker/docker/utils/templates"
- "github.com/docker/engine-api/types"
- "github.com/docker/engine-api/types/filters"
-)
-
-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
-}
-
-// CmdPs outputs a list of Docker containers.
-//
-// Usage: docker ps [OPTIONS]
-func (cli *DockerCli) CmdPs(args ...string) error {
- var (
- err error
-
- psFilterArgs = filters.NewArgs()
-
- cmd = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
- 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{"-no-trunc"}, false, "Don't truncate output")
- 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)")
- format = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
- flFilter = opts.NewListOpts(nil)
- )
- cmd.Require(flag.Exact, 0)
-
- cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
-
- cmd.ParseFlags(args, true)
- 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
- }
- }
-
- options := types.ContainerListOptions{
- All: *all,
- Limit: *last,
- Size: *size,
- Filter: psFilterArgs,
- }
-
- pre := &preProcessor{opts: &options}
- tmpl, err := templates.Parse(*format)
- if err != nil {
- return err
- }
-
- _ = tmpl.Execute(ioutil.Discard, pre)
-
- containers, err := cli.client.ContainerList(context.Background(), options)
- if err != nil {
- return err
- }
-
- f := *format
- if len(f) == 0 {
- if len(cli.PsFormat()) > 0 && !*quiet {
- f = cli.PsFormat()
- } else {
- f = "table"
- }
- }
-
- psCtx := formatter.ContainerContext{
- Context: formatter.Context{
- Output: cli.out,
- Format: f,
- Quiet: *quiet,
- Trunc: !*noTrunc,
- },
- Size: *size,
- Containers: containers,
- }
-
- psCtx.Write()
-
- return nil
-}
diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go
index b1980491a8..4d2958b531 100644
--- a/cli/cobraadaptor/adaptor.go
+++ b/cli/cobraadaptor/adaptor.go
@@ -45,6 +45,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
container.NewLogsCommand(dockerCli),
container.NewPauseCommand(dockerCli),
container.NewPortCommand(dockerCli),
+ container.NewPsCommand(dockerCli),
container.NewRenameCommand(dockerCli),
container.NewRestartCommand(dockerCli),
container.NewRmCommand(dockerCli),
diff --git a/cli/usage.go b/cli/usage.go
index 9cd7acd244..73fa4f2245 100644
--- a/cli/usage.go
+++ b/cli/usage.go
@@ -12,7 +12,6 @@ var DockerCommandUsage = []Command{
{"exec", "Run a command in a running container"},
{"info", "Display system-wide information"},
{"inspect", "Return low-level information on a container or image"},
- {"ps", "List containers"},
{"update", "Update configuration of one or more containers"},
}