Skip to content

Commit cb615a9

Browse files
committed
cli/command/formatter: Context.postFormat: remove redundant buffer
A tabwriter is backed by a buffer already, because it needs to re-flow columns based on content written to it. This buffer was added in [moby@ea61dac9e6] as part of a new feature to allow for custom delimiters; neither the patch, nor code-review on the PR mention the extra buffer, so it likely was just overlooked. This patch; - removes the redundant buffer - adds an early return for cases where no tabwriter is used. [moby@ea61dac9e6]: moby/moby@ea61dac Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent f7a909d commit cb615a9

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

cli/command/formatter/formatter.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,21 @@ func (c *Context) parseFormat() (*template.Template, error) {
8282
}
8383

8484
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {
85-
if c.Output == nil {
86-
c.Output = io.Discard
85+
out := c.Output
86+
if out == nil {
87+
out = io.Discard
8788
}
88-
if c.Format.IsTable() {
89-
t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0)
90-
buffer := bytes.NewBufferString("")
91-
tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader())
92-
buffer.WriteTo(t)
93-
t.Write([]byte("\n"))
94-
c.buffer.WriteTo(t)
95-
t.Flush()
96-
} else {
97-
c.buffer.WriteTo(c.Output)
89+
if !c.Format.IsTable() {
90+
_, _ = c.buffer.WriteTo(out)
91+
return
9892
}
93+
94+
// Write column-headers and rows to the tab-writer buffer, then flush the output.
95+
tw := tabwriter.NewWriter(out, 10, 1, 3, ' ', 0)
96+
_ = tmpl.Funcs(templates.HeaderFunctions).Execute(tw, subContext.FullHeader())
97+
_, _ = tw.Write([]byte{'\n'})
98+
_, _ = c.buffer.WriteTo(tw)
99+
_ = tw.Flush()
99100
}
100101

101102
func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error {

0 commit comments

Comments
 (0)