github.com/rentongzhang/docker@v1.8.2-rc1/api/client/ps/formatter.go (about) 1 package ps 2 3 import ( 4 "io" 5 6 "github.com/docker/docker/api/types" 7 ) 8 9 const ( 10 tableFormatKey = "table" 11 rawFormatKey = "raw" 12 13 defaultTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}} ago\t{{.Status}}\t{{.Ports}}\t{{.Names}}" 14 defaultQuietFormat = "{{.ID}}" 15 ) 16 17 type Context struct { 18 Output io.Writer 19 Format string 20 Size bool 21 Quiet bool 22 Trunc bool 23 } 24 25 func Format(ctx Context, containers []types.Container) { 26 switch ctx.Format { 27 case tableFormatKey: 28 tableFormat(ctx, containers) 29 case rawFormatKey: 30 rawFormat(ctx, containers) 31 default: 32 customFormat(ctx, containers) 33 } 34 } 35 36 func rawFormat(ctx Context, containers []types.Container) { 37 if ctx.Quiet { 38 ctx.Format = `container_id: {{.ID}}` 39 } else { 40 ctx.Format = `container_id: {{.ID}} 41 image: {{.Image}} 42 command: {{.Command}} 43 created_at: {{.CreatedAt}} 44 status: {{.Status}} 45 names: {{.Names}} 46 labels: {{.Labels}} 47 ports: {{.Ports}} 48 ` 49 if ctx.Size { 50 ctx.Format += `size: {{.Size}} 51 ` 52 } 53 } 54 55 customFormat(ctx, containers) 56 } 57 58 func tableFormat(ctx Context, containers []types.Container) { 59 ctx.Format = defaultTableFormat 60 if ctx.Quiet { 61 ctx.Format = defaultQuietFormat 62 } 63 64 customFormat(ctx, containers) 65 }