From cb615a9772dd917af327bebcb75c7dc738eafb62 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 20 Mar 2026 11:11:58 +0100 Subject: [PATCH] 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]: https://github.com/moby/moby/commit/ea61dac9e6d04879445f9c34729055ac1bb15050 Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/formatter.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index 24142b21a0..c7c5b36205 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -82,20 +82,21 @@ func (c *Context) parseFormat() (*template.Template, error) { } func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) { - if c.Output == nil { - c.Output = io.Discard + out := c.Output + if out == nil { + out = io.Discard } - if c.Format.IsTable() { - t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0) - buffer := bytes.NewBufferString("") - tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader()) - buffer.WriteTo(t) - t.Write([]byte("\n")) - c.buffer.WriteTo(t) - t.Flush() - } else { - c.buffer.WriteTo(c.Output) + if !c.Format.IsTable() { + _, _ = c.buffer.WriteTo(out) + return } + + // Write column-headers and rows to the tab-writer buffer, then flush the output. + tw := tabwriter.NewWriter(out, 10, 1, 3, ' ', 0) + _ = tmpl.Funcs(templates.HeaderFunctions).Execute(tw, subContext.FullHeader()) + _, _ = tw.Write([]byte{'\n'}) + _, _ = c.buffer.WriteTo(tw) + _ = tw.Flush() } func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error {