github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/formatter/formatter.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  	"text/tabwriter"
     9  	"text/template"
    10  
    11  	"github.com/docker/docker/utils/templates"
    12  )
    13  
    14  const (
    15  	tableFormatKey = "table"
    16  	rawFormatKey   = "raw"
    17  
    18  	defaultQuietFormat = "{{.ID}}"
    19  )
    20  
    21  // Context contains information required by the formatter to print the output as desired.
    22  type Context struct {
    23  	// Output is the output stream to which the formatted string is written.
    24  	Output io.Writer
    25  	// Format is used to choose raw, table or custom format for the output.
    26  	Format string
    27  	// Quiet when set to true will simply print minimal information.
    28  	Quiet bool
    29  	// Trunc when set to true will truncate the output of certain fields such as Container ID.
    30  	Trunc bool
    31  
    32  	// internal element
    33  	table       bool
    34  	finalFormat string
    35  	header      string
    36  	buffer      *bytes.Buffer
    37  }
    38  
    39  func (c *Context) preformat() {
    40  	c.finalFormat = c.Format
    41  
    42  	if strings.HasPrefix(c.Format, tableKey) {
    43  		c.table = true
    44  		c.finalFormat = c.finalFormat[len(tableKey):]
    45  	}
    46  
    47  	c.finalFormat = strings.Trim(c.finalFormat, " ")
    48  	r := strings.NewReplacer(`\t`, "\t", `\n`, "\n")
    49  	c.finalFormat = r.Replace(c.finalFormat)
    50  }
    51  
    52  func (c *Context) parseFormat() (*template.Template, error) {
    53  	tmpl, err := templates.Parse(c.finalFormat)
    54  	if err != nil {
    55  		c.buffer.WriteString(fmt.Sprintf("Template parsing error: %v\n", err))
    56  		c.buffer.WriteTo(c.Output)
    57  	}
    58  	return tmpl, err
    59  }
    60  
    61  func (c *Context) postformat(tmpl *template.Template, subContext subContext) {
    62  	if c.table {
    63  		if len(c.header) == 0 {
    64  			// if we still don't have a header, we didn't have any containers so we need to fake it to get the right headers from the template
    65  			tmpl.Execute(bytes.NewBufferString(""), subContext)
    66  			c.header = subContext.fullHeader()
    67  		}
    68  
    69  		t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
    70  		t.Write([]byte(c.header))
    71  		t.Write([]byte("\n"))
    72  		c.buffer.WriteTo(t)
    73  		t.Flush()
    74  	} else {
    75  		c.buffer.WriteTo(c.Output)
    76  	}
    77  }
    78  
    79  func (c *Context) contextFormat(tmpl *template.Template, subContext subContext) error {
    80  	if err := tmpl.Execute(c.buffer, subContext); err != nil {
    81  		c.buffer = bytes.NewBufferString(fmt.Sprintf("Template parsing error: %v\n", err))
    82  		c.buffer.WriteTo(c.Output)
    83  		return err
    84  	}
    85  	if c.table && len(c.header) == 0 {
    86  		c.header = subContext.fullHeader()
    87  	}
    88  	c.buffer.WriteString("\n")
    89  	return nil
    90  }