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 <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-06-06 06:38:43 -07:00
parent 07a7c0632f
commit 0f38669267
5 changed files with 60 additions and 43 deletions

View File

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

View File

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

View File

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

View File

@ -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),

View File

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