github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/top.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"text/tabwriter"
     7  
     8  	Cli "github.com/docker/docker/cli"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  )
    11  
    12  // CmdTop displays the running processes of a container.
    13  //
    14  // Usage: docker top CONTAINER
    15  func (cli *DockerCli) CmdTop(args ...string) error {
    16  	cmd := Cli.Subcmd("top", []string{"CONTAINER [ps OPTIONS]"}, Cli.DockerCommands["top"].Description, true)
    17  	cmd.Require(flag.Min, 1)
    18  
    19  	cmd.ParseFlags(args, true)
    20  
    21  	var arguments []string
    22  	if cmd.NArg() > 1 {
    23  		arguments = cmd.Args()[1:]
    24  	}
    25  
    26  	procList, err := cli.client.ContainerTop(cmd.Arg(0), arguments)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
    32  	fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
    33  
    34  	for _, proc := range procList.Processes {
    35  		fmt.Fprintln(w, strings.Join(proc, "\t"))
    36  	}
    37  	w.Flush()
    38  	return nil
    39  }