github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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  // Context contains information required by the formatter to print the output as desired.
    18  type Context struct {
    19  	// Output is the output stream to which the formatted string is written.
    20  	Output io.Writer
    21  	// Format is used to choose raw, table or custom format for the output.
    22  	Format string
    23  	// Size when set to true will display the size of the output.
    24  	Size bool
    25  	// Quiet when set to true will simply print minimal information.
    26  	Quiet bool
    27  	// Trunc when set to true will truncate the output of certain fields such as Container ID.
    28  	Trunc bool
    29  }
    30  
    31  // Format helps to format the output using the parameters set in the Context.
    32  // Currently Format allow to display in raw, table or custom format the output.
    33  func Format(ctx Context, containers []types.Container) {
    34  	switch ctx.Format {
    35  	case tableFormatKey:
    36  		tableFormat(ctx, containers)
    37  	case rawFormatKey:
    38  		rawFormat(ctx, containers)
    39  	default:
    40  		customFormat(ctx, containers)
    41  	}
    42  }
    43  
    44  func rawFormat(ctx Context, containers []types.Container) {
    45  	if ctx.Quiet {
    46  		ctx.Format = `container_id: {{.ID}}`
    47  	} else {
    48  		ctx.Format = `container_id: {{.ID}}
    49  image: {{.Image}}
    50  command: {{.Command}}
    51  created_at: {{.CreatedAt}}
    52  status: {{.Status}}
    53  names: {{.Names}}
    54  labels: {{.Labels}}
    55  ports: {{.Ports}}
    56  `
    57  		if ctx.Size {
    58  			ctx.Format += `size: {{.Size}}
    59  `
    60  		}
    61  	}
    62  
    63  	customFormat(ctx, containers)
    64  }
    65  
    66  func tableFormat(ctx Context, containers []types.Container) {
    67  	ctx.Format = defaultTableFormat
    68  	if ctx.Quiet {
    69  		ctx.Format = defaultQuietFormat
    70  	}
    71  
    72  	customFormat(ctx, containers)
    73  }