github.com/a4a881d4/docker@v1.9.0-rc2/api/client/ps/custom.go (about)

     1  package ps
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/api"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/pkg/stringid"
    12  	"github.com/docker/docker/pkg/stringutils"
    13  	"github.com/docker/docker/pkg/units"
    14  )
    15  
    16  const (
    17  	tableKey = "table"
    18  
    19  	idHeader         = "CONTAINER ID"
    20  	imageHeader      = "IMAGE"
    21  	namesHeader      = "NAMES"
    22  	commandHeader    = "COMMAND"
    23  	createdAtHeader  = "CREATED AT"
    24  	runningForHeader = "CREATED"
    25  	statusHeader     = "STATUS"
    26  	portsHeader      = "PORTS"
    27  	sizeHeader       = "SIZE"
    28  	labelsHeader     = "LABELS"
    29  )
    30  
    31  type containerContext struct {
    32  	trunc  bool
    33  	header []string
    34  	c      types.Container
    35  }
    36  
    37  func (c *containerContext) ID() string {
    38  	c.addHeader(idHeader)
    39  	if c.trunc {
    40  		return stringid.TruncateID(c.c.ID)
    41  	}
    42  	return c.c.ID
    43  }
    44  
    45  func (c *containerContext) Names() string {
    46  	c.addHeader(namesHeader)
    47  	names := stripNamePrefix(c.c.Names)
    48  	if c.trunc {
    49  		for _, name := range names {
    50  			if len(strings.Split(name, "/")) == 1 {
    51  				names = []string{name}
    52  				break
    53  			}
    54  		}
    55  	}
    56  	return strings.Join(names, ",")
    57  }
    58  
    59  func (c *containerContext) Image() string {
    60  	c.addHeader(imageHeader)
    61  	if c.c.Image == "" {
    62  		return "<no image>"
    63  	}
    64  	if c.trunc {
    65  		if stringid.TruncateID(c.c.ImageID) == stringid.TruncateID(c.c.Image) {
    66  			return stringutils.Truncate(c.c.Image, 12)
    67  		}
    68  	}
    69  	return c.c.Image
    70  }
    71  
    72  func (c *containerContext) Command() string {
    73  	c.addHeader(commandHeader)
    74  	command := c.c.Command
    75  	if c.trunc {
    76  		command = stringutils.Truncate(command, 20)
    77  	}
    78  	return strconv.Quote(command)
    79  }
    80  
    81  func (c *containerContext) CreatedAt() string {
    82  	c.addHeader(createdAtHeader)
    83  	return time.Unix(int64(c.c.Created), 0).String()
    84  }
    85  
    86  func (c *containerContext) RunningFor() string {
    87  	c.addHeader(runningForHeader)
    88  	createdAt := time.Unix(int64(c.c.Created), 0)
    89  	return units.HumanDuration(time.Now().UTC().Sub(createdAt))
    90  }
    91  
    92  func (c *containerContext) Ports() string {
    93  	c.addHeader(portsHeader)
    94  	return api.DisplayablePorts(c.c.Ports)
    95  }
    96  
    97  func (c *containerContext) Status() string {
    98  	c.addHeader(statusHeader)
    99  	return c.c.Status
   100  }
   101  
   102  func (c *containerContext) Size() string {
   103  	c.addHeader(sizeHeader)
   104  	srw := units.HumanSize(float64(c.c.SizeRw))
   105  	sv := units.HumanSize(float64(c.c.SizeRootFs))
   106  
   107  	sf := srw
   108  	if c.c.SizeRootFs > 0 {
   109  		sf = fmt.Sprintf("%s (virtual %s)", srw, sv)
   110  	}
   111  	return sf
   112  }
   113  
   114  func (c *containerContext) Labels() string {
   115  	c.addHeader(labelsHeader)
   116  	if c.c.Labels == nil {
   117  		return ""
   118  	}
   119  
   120  	var joinLabels []string
   121  	for k, v := range c.c.Labels {
   122  		joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
   123  	}
   124  	return strings.Join(joinLabels, ",")
   125  }
   126  
   127  func (c *containerContext) Label(name string) string {
   128  	n := strings.Split(name, ".")
   129  	r := strings.NewReplacer("-", " ", "_", " ")
   130  	h := r.Replace(n[len(n)-1])
   131  
   132  	c.addHeader(h)
   133  
   134  	if c.c.Labels == nil {
   135  		return ""
   136  	}
   137  	return c.c.Labels[name]
   138  }
   139  
   140  func (c *containerContext) fullHeader() string {
   141  	if c.header == nil {
   142  		return ""
   143  	}
   144  	return strings.Join(c.header, "\t")
   145  }
   146  
   147  func (c *containerContext) addHeader(header string) {
   148  	if c.header == nil {
   149  		c.header = []string{}
   150  	}
   151  	c.header = append(c.header, strings.ToUpper(header))
   152  }
   153  
   154  func stripNamePrefix(ss []string) []string {
   155  	for i, s := range ss {
   156  		ss[i] = s[1:]
   157  	}
   158  
   159  	return ss
   160  }