2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
|
|
|
|
2017-08-08 11:26:24 -04:00
|
|
|
"github.com/docker/cli/templates"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2016-07-25 15:24:34 -04:00
|
|
|
// Format keys used to specify certain kinds of output formats
|
2016-09-08 13:11:39 -04:00
|
|
|
const (
|
2016-07-25 15:24:34 -04:00
|
|
|
TableFormatKey = "table"
|
|
|
|
RawFormatKey = "raw"
|
|
|
|
PrettyFormatKey = "pretty"
|
2021-01-18 11:09:21 -05:00
|
|
|
JSONFormatKey = "json"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
DefaultQuietFormat = "{{.ID}}"
|
2021-01-18 11:09:21 -05:00
|
|
|
jsonFormat = "{{json .}}"
|
2016-09-08 13:11:39 -04: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)
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:09:21 -05:00
|
|
|
// IsJSON returns true if the format is the json format
|
|
|
|
func (f Format) IsJSON() bool {
|
|
|
|
return string(f) == JSONFormatKey
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// Contains returns true if the format contains the substring
|
|
|
|
func (f Format) Contains(sub string) bool {
|
|
|
|
return strings.Contains(string(f), sub)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04: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
|
2016-09-08 13:11:39 -04: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{}
|
2016-09-08 13:11:39 -04:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
func (c *Context) preFormat() {
|
|
|
|
c.finalFormat = string(c.Format)
|
|
|
|
// TODO: handle this in the Format type
|
2021-01-18 11:09:21 -05:00
|
|
|
switch {
|
|
|
|
case c.Format.IsTable():
|
2016-09-12 16:59:18 -04:00
|
|
|
c.finalFormat = c.finalFormat[len(TableFormatKey):]
|
2021-01-18 11:09:21 -05:00
|
|
|
case c.Format.IsJSON():
|
|
|
|
c.finalFormat = jsonFormat
|
2016-09-08 13:11:39 -04: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) {
|
|
|
|
tmpl, err := templates.Parse(c.finalFormat)
|
|
|
|
if err != nil {
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
return tmpl, errors.Wrap(err, "template parsing error")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return tmpl, err
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {
|
2016-09-12 16:59:18 -04:00
|
|
|
if c.Format.IsTable() {
|
2020-08-28 17:00:21 -04:00
|
|
|
t := tabwriter.NewWriter(c.Output, 10, 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)
|
2016-09-08 13:11:39 -04:00
|
|
|
t.Write([]byte("\n"))
|
|
|
|
c.buffer.WriteTo(t)
|
|
|
|
t.Flush()
|
|
|
|
} else {
|
|
|
|
c.buffer.WriteTo(c.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
if err := tmpl.Execute(c.buffer, subContext); err != nil {
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
return errors.Wrap(err, "template parsing error")
|
2016-09-08 13:11:39 -04: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()
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
c.buffer.WriteString("\n")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
|
|
|
|
// SubFormat is a function type accepted by Write()
|
2018-10-23 11:05:44 -04:00
|
|
|
type SubFormat func(func(SubContext) error) error
|
2016-09-12 16:59:18 -04:00
|
|
|
|
|
|
|
// Write the template to the buffer using this Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func (c *Context) Write(sub SubContext, f SubFormat) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.buffer = bytes.NewBufferString("")
|
|
|
|
c.preFormat()
|
|
|
|
|
|
|
|
tmpl, err := c.parseFormat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
subFormat := func(subContext SubContext) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
return c.contextFormat(tmpl, subContext)
|
|
|
|
}
|
|
|
|
if err := f(subFormat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.postFormat(tmpl, sub)
|
|
|
|
return nil
|
|
|
|
}
|