Jelajahi Sumber

Migrate ps command to cobra

Signed-off-by: Tianyi Wang <capkurmagati@gmail.com>
Tianyi Wang 9 tahun lalu
induk
melakukan
5a0b53b5a3
5 mengubah file dengan 126 tambahan dan 107 penghapusan
  1. 0 1
      api/client/commands.go
  2. 125 0
      api/client/container/ps.go
  3. 0 105
      api/client/ps.go
  4. 1 0
      cli/cobraadaptor/adaptor.go
  5. 0 1
      cli/usage.go

+ 0 - 1
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]
 }

+ 125 - 0
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
+}

+ 0 - 105
api/client/ps.go

@@ -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
-}

+ 1 - 0
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),

+ 0 - 1
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"},
 }