github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/top.go (about)

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