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

     1  package client
     2  
     3  import (
     4  	"github.com/docker/docker/api/client/formatter"
     5  	Cli "github.com/docker/docker/cli"
     6  	"github.com/docker/docker/opts"
     7  	flag "github.com/docker/docker/pkg/mflag"
     8  	"github.com/docker/engine-api/types"
     9  	"github.com/docker/engine-api/types/filters"
    10  )
    11  
    12  // CmdPs outputs a list of Docker containers.
    13  //
    14  // Usage: docker ps [OPTIONS]
    15  func (cli *DockerCli) CmdPs(args ...string) error {
    16  	var (
    17  		err error
    18  
    19  		psFilterArgs = filters.NewArgs()
    20  
    21  		cmd      = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
    22  		quiet    = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
    23  		size     = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
    24  		all      = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
    25  		noTrunc  = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
    26  		nLatest  = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)")
    27  		since    = cmd.String([]string{"#-since"}, "", "Show containers created since Id or Name (includes all states)")
    28  		before   = cmd.String([]string{"#-before"}, "", "Only show containers created before Id or Name")
    29  		last     = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)")
    30  		format   = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
    31  		flFilter = opts.NewListOpts(nil)
    32  	)
    33  	cmd.Require(flag.Exact, 0)
    34  
    35  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
    36  
    37  	cmd.ParseFlags(args, true)
    38  	if *last == -1 && *nLatest {
    39  		*last = 1
    40  	}
    41  
    42  	// Consolidate all filter flags, and sanity check them.
    43  	// They'll get processed in the daemon/server.
    44  	for _, f := range flFilter.GetAll() {
    45  		if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	options := types.ContainerListOptions{
    51  		All:    *all,
    52  		Limit:  *last,
    53  		Since:  *since,
    54  		Before: *before,
    55  		Size:   *size,
    56  		Filter: psFilterArgs,
    57  	}
    58  
    59  	containers, err := cli.client.ContainerList(options)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	f := *format
    65  	if len(f) == 0 {
    66  		if len(cli.PsFormat()) > 0 && !*quiet {
    67  			f = cli.PsFormat()
    68  		} else {
    69  			f = "table"
    70  		}
    71  	}
    72  
    73  	psCtx := formatter.ContainerContext{
    74  		Context: formatter.Context{
    75  			Output: cli.out,
    76  			Format: f,
    77  			Quiet:  *quiet,
    78  			Trunc:  !*noTrunc,
    79  		},
    80  		Size:       *size,
    81  		Containers: containers,
    82  	}
    83  
    84  	psCtx.Write()
    85  
    86  	return nil
    87  }