github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/api/client/ps.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"strconv"
     8  	"strings"
     9  	"text/tabwriter"
    10  	"time"
    11  
    12  	"github.com/docker/docker/api"
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/opts"
    15  	flag "github.com/docker/docker/pkg/mflag"
    16  	"github.com/docker/docker/pkg/parsers/filters"
    17  	"github.com/docker/docker/pkg/stringid"
    18  	"github.com/docker/docker/pkg/stringutils"
    19  	"github.com/docker/docker/pkg/units"
    20  )
    21  
    22  // CmdPs outputs a list of Docker containers.
    23  //
    24  // Usage: docker ps [OPTIONS]
    25  func (cli *DockerCli) CmdPs(args ...string) error {
    26  	var (
    27  		err error
    28  
    29  		psFilterArgs = filters.Args{}
    30  		v            = url.Values{}
    31  
    32  		cmd      = cli.Subcmd("ps", "", "List containers", true)
    33  		quiet    = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
    34  		size     = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
    35  		all      = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
    36  		noTrunc  = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
    37  		nLatest  = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container, include non-running")
    38  		since    = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show created since Id or Name, include non-running")
    39  		before   = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name")
    40  		last     = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running")
    41  		flFilter = opts.NewListOpts(nil)
    42  	)
    43  	cmd.Require(flag.Exact, 0)
    44  
    45  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
    46  
    47  	cmd.ParseFlags(args, true)
    48  	if *last == -1 && *nLatest {
    49  		*last = 1
    50  	}
    51  
    52  	if *all {
    53  		v.Set("all", "1")
    54  	}
    55  
    56  	if *last != -1 {
    57  		v.Set("limit", strconv.Itoa(*last))
    58  	}
    59  
    60  	if *since != "" {
    61  		v.Set("since", *since)
    62  	}
    63  
    64  	if *before != "" {
    65  		v.Set("before", *before)
    66  	}
    67  
    68  	if *size {
    69  		v.Set("size", "1")
    70  	}
    71  
    72  	// Consolidate all filter flags, and sanity check them.
    73  	// They'll get processed in the daemon/server.
    74  	for _, f := range flFilter.GetAll() {
    75  		if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
    76  			return err
    77  		}
    78  	}
    79  
    80  	if len(psFilterArgs) > 0 {
    81  		filterJSON, err := filters.ToParam(psFilterArgs)
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		v.Set("filters", filterJSON)
    87  	}
    88  
    89  	rdr, _, err := cli.call("GET", "/containers/json?"+v.Encode(), nil, nil)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	containers := []types.Container{}
    95  	if err := json.NewDecoder(rdr).Decode(&containers); err != nil {
    96  		return err
    97  	}
    98  
    99  	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
   100  	if !*quiet {
   101  		fmt.Fprint(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES")
   102  
   103  		if *size {
   104  			fmt.Fprintln(w, "\tSIZE")
   105  		} else {
   106  			fmt.Fprint(w, "\n")
   107  		}
   108  	}
   109  
   110  	stripNamePrefix := func(ss []string) []string {
   111  		for i, s := range ss {
   112  			ss[i] = s[1:]
   113  		}
   114  
   115  		return ss
   116  	}
   117  
   118  	for _, container := range containers {
   119  		ID := container.ID
   120  
   121  		if !*noTrunc {
   122  			ID = stringid.TruncateID(ID)
   123  		}
   124  
   125  		if *quiet {
   126  			fmt.Fprintln(w, ID)
   127  
   128  			continue
   129  		}
   130  
   131  		var (
   132  			names   = stripNamePrefix(container.Names)
   133  			command = strconv.Quote(container.Command)
   134  		)
   135  
   136  		if !*noTrunc {
   137  			command = stringutils.Truncate(command, 20)
   138  
   139  			// only display the default name for the container with notrunc is passed
   140  			for _, name := range names {
   141  				if len(strings.Split(name, "/")) == 1 {
   142  					names = []string{name}
   143  					break
   144  				}
   145  			}
   146  		}
   147  
   148  		image := container.Image
   149  		if image == "" {
   150  			image = "<no image>"
   151  		}
   152  
   153  		fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", ID, image, command,
   154  			units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(container.Created), 0))),
   155  			container.Status, api.DisplayablePorts(container.Ports), strings.Join(names, ","))
   156  
   157  		if *size {
   158  			if container.SizeRootFs > 0 {
   159  				fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(float64(container.SizeRw)), units.HumanSize(float64(container.SizeRootFs)))
   160  			} else {
   161  				fmt.Fprintf(w, "%s\n", units.HumanSize(float64(container.SizeRw)))
   162  			}
   163  
   164  			continue
   165  		}
   166  
   167  		fmt.Fprint(w, "\n")
   168  	}
   169  
   170  	if !*quiet {
   171  		w.Flush()
   172  	}
   173  
   174  	return nil
   175  }