github.com/rentongzhang/docker@v1.8.2-rc1/api/client/ps.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/docker/docker/api/client/ps"
     9  	"github.com/docker/docker/api/types"
    10  	Cli "github.com/docker/docker/cli"
    11  	"github.com/docker/docker/opts"
    12  	flag "github.com/docker/docker/pkg/mflag"
    13  	"github.com/docker/docker/pkg/parsers/filters"
    14  )
    15  
    16  // CmdPs outputs a list of Docker containers.
    17  //
    18  // Usage: docker ps [OPTIONS]
    19  func (cli *DockerCli) CmdPs(args ...string) error {
    20  	var (
    21  		err error
    22  
    23  		psFilterArgs = filters.Args{}
    24  		v            = url.Values{}
    25  
    26  		cmd      = Cli.Subcmd("ps", nil, "List containers", true)
    27  		quiet    = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
    28  		size     = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
    29  		all      = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
    30  		noTrunc  = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
    31  		nLatest  = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container, include non-running")
    32  		since    = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show created since Id or Name, include non-running")
    33  		before   = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name")
    34  		last     = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running")
    35  		format   = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
    36  		flFilter = opts.NewListOpts(nil)
    37  	)
    38  	cmd.Require(flag.Exact, 0)
    39  
    40  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
    41  
    42  	cmd.ParseFlags(args, true)
    43  	if *last == -1 && *nLatest {
    44  		*last = 1
    45  	}
    46  
    47  	if *all {
    48  		v.Set("all", "1")
    49  	}
    50  
    51  	if *last != -1 {
    52  		v.Set("limit", strconv.Itoa(*last))
    53  	}
    54  
    55  	if *since != "" {
    56  		v.Set("since", *since)
    57  	}
    58  
    59  	if *before != "" {
    60  		v.Set("before", *before)
    61  	}
    62  
    63  	if *size {
    64  		v.Set("size", "1")
    65  	}
    66  
    67  	// Consolidate all filter flags, and sanity check them.
    68  	// They'll get processed in the daemon/server.
    69  	for _, f := range flFilter.GetAll() {
    70  		if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	if len(psFilterArgs) > 0 {
    76  		filterJSON, err := filters.ToParam(psFilterArgs)
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		v.Set("filters", filterJSON)
    82  	}
    83  
    84  	serverResp, err := cli.call("GET", "/containers/json?"+v.Encode(), nil, nil)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	defer serverResp.body.Close()
    90  
    91  	containers := []types.Container{}
    92  	if err := json.NewDecoder(serverResp.body).Decode(&containers); err != nil {
    93  		return err
    94  	}
    95  
    96  	f := *format
    97  	if len(f) == 0 {
    98  		if len(cli.PsFormat()) > 0 && !*quiet {
    99  			f = cli.PsFormat()
   100  		} else {
   101  			f = "table"
   102  		}
   103  	}
   104  
   105  	psCtx := ps.Context{
   106  		Output: cli.out,
   107  		Format: f,
   108  		Quiet:  *quiet,
   109  		Size:   *size,
   110  		Trunc:  !*noTrunc,
   111  	}
   112  
   113  	ps.Format(psCtx, containers)
   114  
   115  	return nil
   116  }