github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/formatter/custom.go (about) 1 package formatter 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/api" 10 "github.com/docker/docker/pkg/stringid" 11 "github.com/docker/docker/pkg/stringutils" 12 "github.com/docker/engine-api/types" 13 "github.com/docker/go-units" 14 ) 15 16 const ( 17 tableKey = "table" 18 19 containerIDHeader = "CONTAINER ID" 20 imageHeader = "IMAGE" 21 namesHeader = "NAMES" 22 commandHeader = "COMMAND" 23 createdSinceHeader = "CREATED" 24 createdAtHeader = "CREATED AT" 25 runningForHeader = "CREATED" 26 statusHeader = "STATUS" 27 portsHeader = "PORTS" 28 sizeHeader = "SIZE" 29 labelsHeader = "LABELS" 30 imageIDHeader = "IMAGE ID" 31 repositoryHeader = "REPOSITORY" 32 tagHeader = "TAG" 33 digestHeader = "DIGEST" 34 ) 35 36 type containerContext struct { 37 baseSubContext 38 trunc bool 39 c types.Container 40 } 41 42 func (c *containerContext) ID() string { 43 c.addHeader(containerIDHeader) 44 if c.trunc { 45 return stringid.TruncateID(c.c.ID) 46 } 47 return c.c.ID 48 } 49 50 func (c *containerContext) Names() string { 51 c.addHeader(namesHeader) 52 names := stripNamePrefix(c.c.Names) 53 if c.trunc { 54 for _, name := range names { 55 if len(strings.Split(name, "/")) == 1 { 56 names = []string{name} 57 break 58 } 59 } 60 } 61 return strings.Join(names, ",") 62 } 63 64 func (c *containerContext) Image() string { 65 c.addHeader(imageHeader) 66 if c.c.Image == "" { 67 return "<no image>" 68 } 69 if c.trunc { 70 if trunc := stringid.TruncateID(c.c.ImageID); trunc == stringid.TruncateID(c.c.Image) { 71 return trunc 72 } 73 } 74 return c.c.Image 75 } 76 77 func (c *containerContext) Command() string { 78 c.addHeader(commandHeader) 79 command := c.c.Command 80 if c.trunc { 81 command = stringutils.Truncate(command, 20) 82 } 83 return strconv.Quote(command) 84 } 85 86 func (c *containerContext) CreatedAt() string { 87 c.addHeader(createdAtHeader) 88 return time.Unix(int64(c.c.Created), 0).String() 89 } 90 91 func (c *containerContext) RunningFor() string { 92 c.addHeader(runningForHeader) 93 createdAt := time.Unix(int64(c.c.Created), 0) 94 return units.HumanDuration(time.Now().UTC().Sub(createdAt)) 95 } 96 97 func (c *containerContext) Ports() string { 98 c.addHeader(portsHeader) 99 return api.DisplayablePorts(c.c.Ports) 100 } 101 102 func (c *containerContext) Status() string { 103 c.addHeader(statusHeader) 104 return c.c.Status 105 } 106 107 func (c *containerContext) Size() string { 108 c.addHeader(sizeHeader) 109 srw := units.HumanSize(float64(c.c.SizeRw)) 110 sv := units.HumanSize(float64(c.c.SizeRootFs)) 111 112 sf := srw 113 if c.c.SizeRootFs > 0 { 114 sf = fmt.Sprintf("%s (virtual %s)", srw, sv) 115 } 116 return sf 117 } 118 119 func (c *containerContext) Labels() string { 120 c.addHeader(labelsHeader) 121 if c.c.Labels == nil { 122 return "" 123 } 124 125 var joinLabels []string 126 for k, v := range c.c.Labels { 127 joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) 128 } 129 return strings.Join(joinLabels, ",") 130 } 131 132 func (c *containerContext) Label(name string) string { 133 n := strings.Split(name, ".") 134 r := strings.NewReplacer("-", " ", "_", " ") 135 h := r.Replace(n[len(n)-1]) 136 137 c.addHeader(h) 138 139 if c.c.Labels == nil { 140 return "" 141 } 142 return c.c.Labels[name] 143 } 144 145 type imageContext struct { 146 baseSubContext 147 trunc bool 148 i types.Image 149 repo string 150 tag string 151 digest string 152 } 153 154 func (c *imageContext) ID() string { 155 c.addHeader(imageIDHeader) 156 if c.trunc { 157 return stringid.TruncateID(c.i.ID) 158 } 159 return c.i.ID 160 } 161 162 func (c *imageContext) Repository() string { 163 c.addHeader(repositoryHeader) 164 return c.repo 165 } 166 167 func (c *imageContext) Tag() string { 168 c.addHeader(tagHeader) 169 return c.tag 170 } 171 172 func (c *imageContext) Digest() string { 173 c.addHeader(digestHeader) 174 return c.digest 175 } 176 177 func (c *imageContext) CreatedSince() string { 178 c.addHeader(createdSinceHeader) 179 createdAt := time.Unix(int64(c.i.Created), 0) 180 return units.HumanDuration(time.Now().UTC().Sub(createdAt)) 181 } 182 183 func (c *imageContext) CreatedAt() string { 184 c.addHeader(createdAtHeader) 185 return time.Unix(int64(c.i.Created), 0).String() 186 } 187 188 func (c *imageContext) Size() string { 189 c.addHeader(sizeHeader) 190 return units.HumanSize(float64(c.i.Size)) 191 } 192 193 type subContext interface { 194 fullHeader() string 195 addHeader(header string) 196 } 197 198 type baseSubContext struct { 199 header []string 200 } 201 202 func (c *baseSubContext) fullHeader() string { 203 if c.header == nil { 204 return "" 205 } 206 return strings.Join(c.header, "\t") 207 } 208 209 func (c *baseSubContext) addHeader(header string) { 210 if c.header == nil { 211 c.header = []string{} 212 } 213 c.header = append(c.header, strings.ToUpper(header)) 214 } 215 216 func stripNamePrefix(ss []string) []string { 217 for i, s := range ss { 218 ss[i] = s[1:] 219 } 220 221 return ss 222 }