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

Use spf13/cobra for docker top
This commit is contained in:
Alexander Morozov 2016-06-06 10:14:10 -07:00
commit 5ce2f14f9b
5 changed files with 60 additions and 43 deletions

View file

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

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

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

View file

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