2015-12-18 08:03:41 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
|
|
|
|
2016-12-12 03:34:03 -05:00
|
|
|
"github.com/docker/docker/pkg/templates"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2015-12-18 08:03:41 -05:00
|
|
|
)
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
// Format keys used to specify certain kinds of output formats
|
2015-12-18 08:03:41 -05:00
|
|
|
const (
|
2016-07-25 15:24:34 -04:00
|
|
|
TableFormatKey = "table"
|
|
|
|
RawFormatKey = "raw"
|
|
|
|
PrettyFormatKey = "pretty"
|
2015-12-18 08:03:41 -05:00
|
|
|
|
2016-08-04 08:59:45 -04:00
|
|
|
defaultQuietFormat = "{{.ID}}"
|
2015-12-18 08:03:41 -05:00
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// Format is the format string rendered using the Context
|
|
|
|
type Format string
|
|
|
|
|
|
|
|
// IsTable returns true if the format is a table-type format
|
|
|
|
func (f Format) IsTable() bool {
|
|
|
|
return strings.HasPrefix(string(f), TableFormatKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns true if the format contains the substring
|
|
|
|
func (f Format) Contains(sub string) bool {
|
|
|
|
return strings.Contains(string(f), sub)
|
|
|
|
}
|
|
|
|
|
2015-12-18 08:03:41 -05:00
|
|
|
// Context contains information required by the formatter to print the output as desired.
|
|
|
|
type Context struct {
|
|
|
|
// Output is the output stream to which the formatted string is written.
|
|
|
|
Output io.Writer
|
|
|
|
// Format is used to choose raw, table or custom format for the output.
|
2016-09-12 16:59:18 -04:00
|
|
|
Format Format
|
2015-12-18 08:03:41 -05:00
|
|
|
// Trunc when set to true will truncate the output of certain fields such as Container ID.
|
|
|
|
Trunc bool
|
|
|
|
|
|
|
|
// internal element
|
|
|
|
finalFormat string
|
2017-02-03 19:48:46 -05:00
|
|
|
header interface{}
|
2015-12-18 08:03:41 -05:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
func (c *Context) preFormat() {
|
|
|
|
c.finalFormat = string(c.Format)
|
2015-12-18 08:03:41 -05:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// TODO: handle this in the Format type
|
|
|
|
if c.Format.IsTable() {
|
|
|
|
c.finalFormat = c.finalFormat[len(TableFormatKey):]
|
2015-12-18 08:03:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c.finalFormat = strings.Trim(c.finalFormat, " ")
|
|
|
|
r := strings.NewReplacer(`\t`, "\t", `\n`, "\n")
|
|
|
|
c.finalFormat = r.Replace(c.finalFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) parseFormat() (*template.Template, error) {
|
2016-03-04 12:29:44 -05:00
|
|
|
tmpl, err := templates.Parse(c.finalFormat)
|
2015-12-18 08:03:41 -05:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
|
2015-12-18 08:03:41 -05:00
|
|
|
}
|
|
|
|
return tmpl, err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
func (c *Context) postFormat(tmpl *template.Template, subContext subContext) {
|
|
|
|
if c.Format.IsTable() {
|
2015-12-18 08:03:41 -05:00
|
|
|
t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
|
2017-02-03 19:48:46 -05:00
|
|
|
buffer := bytes.NewBufferString("")
|
2017-02-03 23:23:00 -05:00
|
|
|
tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader())
|
2017-02-03 19:48:46 -05:00
|
|
|
buffer.WriteTo(t)
|
2015-12-18 08:03:41 -05:00
|
|
|
t.Write([]byte("\n"))
|
|
|
|
c.buffer.WriteTo(t)
|
|
|
|
t.Flush()
|
|
|
|
} else {
|
|
|
|
c.buffer.WriteTo(c.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) contextFormat(tmpl *template.Template, subContext subContext) error {
|
|
|
|
if err := tmpl.Execute(c.buffer, subContext); err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Template parsing error: %v\n", err)
|
2015-12-18 08:03:41 -05:00
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
if c.Format.IsTable() && c.header != nil {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.header = subContext.FullHeader()
|
2015-12-18 08:03:41 -05:00
|
|
|
}
|
|
|
|
c.buffer.WriteString("\n")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
|
|
|
|
// SubFormat is a function type accepted by Write()
|
|
|
|
type SubFormat func(func(subContext) error) error
|
|
|
|
|
|
|
|
// Write the template to the buffer using this Context
|
|
|
|
func (c *Context) Write(sub subContext, f SubFormat) error {
|
|
|
|
c.buffer = bytes.NewBufferString("")
|
|
|
|
c.preFormat()
|
|
|
|
|
|
|
|
tmpl, err := c.parseFormat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subFormat := func(subContext subContext) error {
|
|
|
|
return c.contextFormat(tmpl, subContext)
|
|
|
|
}
|
|
|
|
if err := f(subFormat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.postFormat(tmpl, sub)
|
|
|
|
return nil
|
|
|
|
}
|