瀏覽代碼

Merge pull request #23306 from yongtang/23211-spf13-cobra-top

Use spf13/cobra for docker top
Alexander Morozov 9 年之前
父節點
當前提交
5ce2f14f9b
共有 5 個文件被更改,包括 60 次插入43 次删除
  1. 0 1
      api/client/commands.go
  2. 59 0
      api/client/container/top.go
  3. 0 41
      api/client/top.go
  4. 1 0
      cli/cobraadaptor/adaptor.go
  5. 0 1
      cli/usage.go

+ 0 - 1
api/client/commands.go

@@ -28,7 +28,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
 		"save":    cli.CmdSave,
 		"stats":   cli.CmdStats,
 		"tag":     cli.CmdTag,
-		"top":     cli.CmdTop,
 		"update":  cli.CmdUpdate,
 		"version": cli.CmdVersion,
 	}[name]

+ 59 - 0
api/client/container/top.go

@@ -0,0 +1,59 @@
+package container
+
+import (
+	"fmt"
+	"strings"
+	"text/tabwriter"
+
+	"golang.org/x/net/context"
+
+	"github.com/docker/docker/api/client"
+	"github.com/docker/docker/cli"
+	"github.com/spf13/cobra"
+)
+
+type topOptions struct {
+	container string
+
+	args []string
+}
+
+// NewTopCommand creats a new cobra.Command for `docker top`
+func NewTopCommand(dockerCli *client.DockerCli) *cobra.Command {
+	var opts topOptions
+
+	cmd := &cobra.Command{
+		Use:   "top CONTAINER [ps OPTIONS]",
+		Short: "Display the running processes of a container",
+		Args:  cli.RequiresMinArgs(1),
+		RunE: func(cmd *cobra.Command, args []string) error {
+			opts.container = args[0]
+			opts.args = args[1:]
+			return runTop(dockerCli, &opts)
+		},
+	}
+	cmd.SetFlagErrorFunc(flagErrorFunc)
+
+	flags := cmd.Flags()
+	flags.SetInterspersed(false)
+
+	return cmd
+}
+
+func runTop(dockerCli *client.DockerCli, opts *topOptions) error {
+	ctx := context.Background()
+
+	procList, err := dockerCli.Client().ContainerTop(ctx, opts.container, opts.args)
+	if err != nil {
+		return err
+	}
+
+	w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
+	fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
+
+	for _, proc := range procList.Processes {
+		fmt.Fprintln(w, strings.Join(proc, "\t"))
+	}
+	w.Flush()
+	return nil
+}

+ 0 - 41
api/client/top.go

@@ -1,41 +0,0 @@
-package client
-
-import (
-	"fmt"
-	"strings"
-	"text/tabwriter"
-
-	"golang.org/x/net/context"
-
-	Cli "github.com/docker/docker/cli"
-	flag "github.com/docker/docker/pkg/mflag"
-)
-
-// CmdTop displays the running processes of a container.
-//
-// Usage: docker top CONTAINER
-func (cli *DockerCli) CmdTop(args ...string) error {
-	cmd := Cli.Subcmd("top", []string{"CONTAINER [ps OPTIONS]"}, Cli.DockerCommands["top"].Description, true)
-	cmd.Require(flag.Min, 1)
-
-	cmd.ParseFlags(args, true)
-
-	var arguments []string
-	if cmd.NArg() > 1 {
-		arguments = cmd.Args()[1:]
-	}
-
-	procList, err := cli.client.ContainerTop(context.Background(), cmd.Arg(0), arguments)
-	if err != nil {
-		return err
-	}
-
-	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
-	fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
-
-	for _, proc := range procList.Processes {
-		fmt.Fprintln(w, strings.Join(proc, "\t"))
-	}
-	w.Flush()
-	return nil
-}

+ 1 - 0
cli/cobraadaptor/adaptor.go

@@ -42,6 +42,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
 		container.NewRunCommand(dockerCli),
 		container.NewStartCommand(dockerCli),
 		container.NewStopCommand(dockerCli),
+		container.NewTopCommand(dockerCli),
 		container.NewUnpauseCommand(dockerCli),
 		container.NewWaitCommand(dockerCli),
 		image.NewRemoveCommand(dockerCli),

+ 0 - 1
cli/usage.go

@@ -33,7 +33,6 @@ var DockerCommandUsage = []Command{
 	{"save", "Save one or more images to a tar archive"},
 	{"stats", "Display a live stream of container(s) resource usage statistics"},
 	{"tag", "Tag an image into a repository"},
-	{"top", "Display the running processes of a container"},
 	{"update", "Update configuration of one or more containers"},
 	{"version", "Show the Docker version information"},
 }