formatter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package formatter
  2. import (
  3. "bytes"
  4. "io"
  5. "strings"
  6. "text/tabwriter"
  7. "text/template"
  8. "github.com/docker/docker/pkg/templates"
  9. "github.com/pkg/errors"
  10. )
  11. // Format keys used to specify certain kinds of output formats
  12. const (
  13. TableFormatKey = "table"
  14. RawFormatKey = "raw"
  15. PrettyFormatKey = "pretty"
  16. defaultQuietFormat = "{{.ID}}"
  17. )
  18. // Format is the format string rendered using the Context
  19. type Format string
  20. // IsTable returns true if the format is a table-type format
  21. func (f Format) IsTable() bool {
  22. return strings.HasPrefix(string(f), TableFormatKey)
  23. }
  24. // Contains returns true if the format contains the substring
  25. func (f Format) Contains(sub string) bool {
  26. return strings.Contains(string(f), sub)
  27. }
  28. // Context contains information required by the formatter to print the output as desired.
  29. type Context struct {
  30. // Output is the output stream to which the formatted string is written.
  31. Output io.Writer
  32. // Format is used to choose raw, table or custom format for the output.
  33. Format Format
  34. // Trunc when set to true will truncate the output of certain fields such as Container ID.
  35. Trunc bool
  36. // internal element
  37. finalFormat string
  38. header interface{}
  39. buffer *bytes.Buffer
  40. }
  41. func (c *Context) preFormat() {
  42. c.finalFormat = string(c.Format)
  43. // TODO: handle this in the Format type
  44. if c.Format.IsTable() {
  45. c.finalFormat = c.finalFormat[len(TableFormatKey):]
  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. func (c *Context) parseFormat() (*template.Template, error) {
  52. tmpl, err := templates.Parse(c.finalFormat)
  53. if err != nil {
  54. return tmpl, errors.Errorf("Template parsing error: %v\n", err)
  55. }
  56. return tmpl, err
  57. }
  58. func (c *Context) postFormat(tmpl *template.Template, subContext subContext) {
  59. if c.Format.IsTable() {
  60. t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
  61. buffer := bytes.NewBufferString("")
  62. tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader())
  63. buffer.WriteTo(t)
  64. t.Write([]byte("\n"))
  65. c.buffer.WriteTo(t)
  66. t.Flush()
  67. } else {
  68. c.buffer.WriteTo(c.Output)
  69. }
  70. }
  71. func (c *Context) contextFormat(tmpl *template.Template, subContext subContext) error {
  72. if err := tmpl.Execute(c.buffer, subContext); err != nil {
  73. return errors.Errorf("Template parsing error: %v\n", err)
  74. }
  75. if c.Format.IsTable() && c.header != nil {
  76. c.header = subContext.FullHeader()
  77. }
  78. c.buffer.WriteString("\n")
  79. return nil
  80. }
  81. // SubFormat is a function type accepted by Write()
  82. type SubFormat func(func(subContext) error) error
  83. // Write the template to the buffer using this Context
  84. func (c *Context) Write(sub subContext, f SubFormat) error {
  85. c.buffer = bytes.NewBufferString("")
  86. c.preFormat()
  87. tmpl, err := c.parseFormat()
  88. if err != nil {
  89. return err
  90. }
  91. subFormat := func(subContext subContext) error {
  92. return c.contextFormat(tmpl, subContext)
  93. }
  94. if err := f(subFormat); err != nil {
  95. return err
  96. }
  97. c.postFormat(tmpl, sub)
  98. return nil
  99. }