2016-08-04 08:59:45 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-08-04 08:59:45 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2016-09-12 16:59:18 -04:00
|
|
|
units "github.com/docker/go-units"
|
2016-08-04 08:59:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}} ago\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
|
|
|
|
|
|
|
|
containerIDHeader = "CONTAINER ID"
|
|
|
|
namesHeader = "NAMES"
|
|
|
|
commandHeader = "COMMAND"
|
|
|
|
runningForHeader = "CREATED"
|
|
|
|
statusHeader = "STATUS"
|
|
|
|
portsHeader = "PORTS"
|
|
|
|
mountsHeader = "MOUNTS"
|
2016-08-23 19:37:37 -04:00
|
|
|
localVolumes = "LOCAL VOLUMES"
|
2016-10-19 18:09:42 -04:00
|
|
|
networksHeader = "NETWORKS"
|
2016-08-04 08:59:45 -04:00
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// NewContainerFormat returns a Format for rendering using a Context
|
|
|
|
func NewContainerFormat(source string, quiet bool, size bool) Format {
|
|
|
|
switch source {
|
|
|
|
case TableFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return defaultQuietFormat
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
format := defaultContainerTableFormat
|
|
|
|
if size {
|
|
|
|
format += `\t{{.Size}}`
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `container_id: {{.ID}}`
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
2016-09-13 14:21:07 -04:00
|
|
|
format := `container_id: {{.ID}}
|
|
|
|
image: {{.Image}}
|
|
|
|
command: {{.Command}}
|
|
|
|
created_at: {{.CreatedAt}}
|
|
|
|
status: {{- pad .Status 1 0}}
|
|
|
|
names: {{.Names}}
|
|
|
|
labels: {{- pad .Labels 1 0}}
|
|
|
|
ports: {{- pad .Ports 1 0}}
|
|
|
|
`
|
2016-09-12 16:59:18 -04:00
|
|
|
if size {
|
|
|
|
format += `size: {{.Size}}\n`
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(source)
|
|
|
|
}
|
2016-08-04 08:59:45 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// ContainerWrite renders the context for a list of containers
|
|
|
|
func ContainerWrite(ctx Context, containers []types.Container) error {
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, container := range containers {
|
|
|
|
err := format(&containerContext{trunc: ctx.Trunc, c: container})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&containerContext{}, render)
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type containerContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
HeaderContext
|
2016-08-04 08:59:45 -04:00
|
|
|
trunc bool
|
|
|
|
c types.Container
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *containerContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return marshalJSON(c)
|
|
|
|
}
|
|
|
|
|
2016-08-04 08:59:45 -04:00
|
|
|
func (c *containerContext) ID() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(containerIDHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.c.ID)
|
|
|
|
}
|
|
|
|
return c.c.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Names() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(namesHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
names := stripNamePrefix(c.c.Names)
|
|
|
|
if c.trunc {
|
|
|
|
for _, name := range names {
|
|
|
|
if len(strings.Split(name, "/")) == 1 {
|
|
|
|
names = []string{name}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Image() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(imageHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
if c.c.Image == "" {
|
|
|
|
return "<no image>"
|
|
|
|
}
|
|
|
|
if c.trunc {
|
|
|
|
if trunc := stringid.TruncateID(c.c.ImageID); trunc == stringid.TruncateID(c.c.Image) {
|
|
|
|
return trunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.c.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Command() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(commandHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
command := c.c.Command
|
|
|
|
if c.trunc {
|
2016-08-10 09:54:14 -04:00
|
|
|
command = stringutils.Ellipsis(command, 20)
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
|
|
|
return strconv.Quote(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) CreatedAt() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(createdAtHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
return time.Unix(int64(c.c.Created), 0).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) RunningFor() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(runningForHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
createdAt := time.Unix(int64(c.c.Created), 0)
|
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(createdAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Ports() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(portsHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
return api.DisplayablePorts(c.c.Ports)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Status() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(statusHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
return c.c.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Size() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(sizeHeader)
|
2016-09-04 17:44:34 -04:00
|
|
|
srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3)
|
|
|
|
sv := units.HumanSizeWithPrecision(float64(c.c.SizeRootFs), 3)
|
2016-08-04 08:59:45 -04:00
|
|
|
|
|
|
|
sf := srw
|
|
|
|
if c.c.SizeRootFs > 0 {
|
|
|
|
sf = fmt.Sprintf("%s (virtual %s)", srw, sv)
|
|
|
|
}
|
|
|
|
return sf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Labels() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(labelsHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var joinLabels []string
|
|
|
|
for k, v := range c.c.Labels {
|
|
|
|
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return strings.Join(joinLabels, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Label(name string) string {
|
|
|
|
n := strings.Split(name, ".")
|
|
|
|
r := strings.NewReplacer("-", " ", "_", " ")
|
|
|
|
h := r.Replace(n[len(n)-1])
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(h)
|
2016-08-04 08:59:45 -04:00
|
|
|
|
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.c.Labels[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *containerContext) Mounts() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(mountsHeader)
|
2016-08-04 08:59:45 -04:00
|
|
|
|
|
|
|
var name string
|
|
|
|
var mounts []string
|
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Name == "" {
|
|
|
|
name = m.Source
|
|
|
|
} else {
|
|
|
|
name = m.Name
|
|
|
|
}
|
|
|
|
if c.trunc {
|
2016-08-10 09:54:14 -04:00
|
|
|
name = stringutils.Ellipsis(name, 15)
|
2016-08-04 08:59:45 -04:00
|
|
|
}
|
|
|
|
mounts = append(mounts, name)
|
|
|
|
}
|
|
|
|
return strings.Join(mounts, ",")
|
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
func (c *containerContext) LocalVolumes() string {
|
|
|
|
c.AddHeader(localVolumes)
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Driver == "local" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d", count)
|
|
|
|
}
|
2016-10-19 18:09:42 -04:00
|
|
|
|
|
|
|
func (c *containerContext) Networks() string {
|
|
|
|
c.AddHeader(networksHeader)
|
|
|
|
|
|
|
|
if c.c.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
networks := []string{}
|
|
|
|
for k := range c.c.NetworkSettings.Networks {
|
|
|
|
networks = append(networks, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(networks, ",")
|
|
|
|
}
|