github.com/portworx/docker@v1.12.1/api/client/container/top.go (about) 1 package container 2 3 import ( 4 "fmt" 5 "strings" 6 "text/tabwriter" 7 8 "golang.org/x/net/context" 9 10 "github.com/docker/docker/api/client" 11 "github.com/docker/docker/cli" 12 "github.com/spf13/cobra" 13 ) 14 15 type topOptions struct { 16 container string 17 18 args []string 19 } 20 21 // NewTopCommand creates a new cobra.Command for `docker top` 22 func NewTopCommand(dockerCli *client.DockerCli) *cobra.Command { 23 var opts topOptions 24 25 cmd := &cobra.Command{ 26 Use: "top CONTAINER [ps OPTIONS]", 27 Short: "Display the running processes of a container", 28 Args: cli.RequiresMinArgs(1), 29 RunE: func(cmd *cobra.Command, args []string) error { 30 opts.container = args[0] 31 opts.args = args[1:] 32 return runTop(dockerCli, &opts) 33 }, 34 } 35 cmd.SetFlagErrorFunc(flagErrorFunc) 36 37 flags := cmd.Flags() 38 flags.SetInterspersed(false) 39 40 return cmd 41 } 42 43 func runTop(dockerCli *client.DockerCli, opts *topOptions) error { 44 ctx := context.Background() 45 46 procList, err := dockerCli.Client().ContainerTop(ctx, opts.container, opts.args) 47 if err != nil { 48 return err 49 } 50 51 w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0) 52 fmt.Fprintln(w, strings.Join(procList.Titles, "\t")) 53 54 for _, proc := range procList.Processes { 55 fmt.Fprintln(w, strings.Join(proc, "\t")) 56 } 57 w.Flush() 58 return nil 59 }