formatter.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package formatter
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "text/tabwriter"
  8. "text/template"
  9. "github.com/docker/docker/utils/templates"
  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 string
  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, fmt.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. if len(c.header) == 0 {
  61. // 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
  62. tmpl.Execute(bytes.NewBufferString(""), subContext)
  63. c.header = subContext.FullHeader()
  64. }
  65. t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
  66. t.Write([]byte(c.header))
  67. t.Write([]byte("\n"))
  68. c.buffer.WriteTo(t)
  69. t.Flush()
  70. } else {
  71. c.buffer.WriteTo(c.Output)
  72. }
  73. }
  74. func (c *Context) contextFormat(tmpl *template.Template, subContext subContext) error {
  75. if err := tmpl.Execute(c.buffer, subContext); err != nil {
  76. return fmt.Errorf("Template parsing error: %v\n", err)
  77. }
  78. if c.Format.IsTable() && len(c.header) == 0 {
  79. c.header = subContext.FullHeader()
  80. }
  81. c.buffer.WriteString("\n")
  82. return nil
  83. }
  84. // SubFormat is a function type accepted by Write()
  85. type SubFormat func(func(subContext) error) error
  86. // Write the template to the buffer using this Context
  87. func (c *Context) Write(sub subContext, f SubFormat) error {
  88. c.buffer = bytes.NewBufferString("")
  89. c.preFormat()
  90. tmpl, err := c.parseFormat()
  91. if err != nil {
  92. return err
  93. }
  94. subFormat := func(subContext subContext) error {
  95. return c.contextFormat(tmpl, subContext)
  96. }
  97. if err := f(subFormat); err != nil {
  98. return err
  99. }
  100. c.postFormat(tmpl, sub)
  101. return nil
  102. }