github.com/tompao/docker@v1.9.1/api/client/top.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/url" 7 "strings" 8 "text/tabwriter" 9 10 "github.com/docker/docker/api/types" 11 Cli "github.com/docker/docker/cli" 12 flag "github.com/docker/docker/pkg/mflag" 13 ) 14 15 // CmdTop displays the running processes of a container. 16 // 17 // Usage: docker top CONTAINER 18 func (cli *DockerCli) CmdTop(args ...string) error { 19 cmd := Cli.Subcmd("top", []string{"CONTAINER [ps OPTIONS]"}, Cli.DockerCommands["top"].Description, true) 20 cmd.Require(flag.Min, 1) 21 22 cmd.ParseFlags(args, true) 23 24 val := url.Values{} 25 if cmd.NArg() > 1 { 26 val.Set("ps_args", strings.Join(cmd.Args()[1:], " ")) 27 } 28 29 serverResp, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/top?"+val.Encode(), nil, nil) 30 if err != nil { 31 return err 32 } 33 34 defer serverResp.body.Close() 35 36 procList := types.ContainerProcessList{} 37 if err := json.NewDecoder(serverResp.body).Decode(&procList); err != nil { 38 return err 39 } 40 41 w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0) 42 fmt.Fprintln(w, strings.Join(procList.Titles, "\t")) 43 44 for _, proc := range procList.Processes { 45 fmt.Fprintln(w, strings.Join(proc, "\t")) 46 } 47 w.Flush() 48 return nil 49 }