diff --git a/api/client/commands.go b/api/client/commands.go index 8387db58d1..2f73a6bd57 100644 --- a/api/client/commands.go +++ b/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] 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 a99f376ca1..ca7d4e54c0 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/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), diff --git a/cli/usage.go b/cli/usage.go index 7ea082629f..f336d611f2 100644 --- a/cli/usage.go +++ b/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"}, }