From 0f3866926763447c247bb24268923b5e50c006e1 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 6 Jun 2016 06:38:43 -0700 Subject: [PATCH] Use spf13/cobra for docker top This fix is part of the effort to convert commands to spf13/cobra #23211. Thif fix coverted command `docker top` to use spf13/cobra Signed-off-by: Yong Tang --- api/client/commands.go | 1 - api/client/container/top.go | 59 +++++++++++++++++++++++++++++++++++++ api/client/top.go | 41 -------------------------- cli/cobraadaptor/adaptor.go | 1 + cli/usage.go | 1 - 5 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 api/client/container/top.go delete mode 100644 api/client/top.go diff --git a/api/client/commands.go b/api/client/commands.go index a1ae5cd5a8..be4341b31f 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -29,7 +29,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, "wait": cli.CmdWait, diff --git a/api/client/container/top.go b/api/client/container/top.go new file mode 100644 index 0000000000..29b4336f7a --- /dev/null +++ b/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 +} diff --git a/api/client/top.go b/api/client/top.go deleted file mode 100644 index bb2ec46c4a..0000000000 --- a/api/client/top.go +++ /dev/null @@ -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 -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index 4ceb57757d..7c839c628e 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -41,6 +41,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewRunCommand(dockerCli), container.NewStartCommand(dockerCli), container.NewStopCommand(dockerCli), + container.NewTopCommand(dockerCli), container.NewUnpauseCommand(dockerCli), image.NewRemoveCommand(dockerCli), image.NewSearchCommand(dockerCli), diff --git a/cli/usage.go b/cli/usage.go index f6152e5c93..31eda11b9b 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -34,7 +34,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"}, {"wait", "Block until a container stops, then print its exit code"},