From 40e7f539141fd493758a89fdf89e2b9a3e60e1cc Mon Sep 17 00:00:00 2001 From: Hooman Date: Wed, 25 Nov 2026 20:28:54 +0330 Subject: [PATCH] formatter: propagate output write errors Return errors from copying formatted rows and flushing the table writer. Keep the existing partial header output for custom table templates. Assisted-by: OpenAI Codex (GPT-6) Signed-off-by: Hooman --- cli/command/formatter/formatter.go | 27 ++++++++++------- cli/command/formatter/formatter_test.go | 40 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index 4beb7f56b256..8b2685ecdf58 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -90,22 +90,30 @@ func (c *Context) parseFormat() (*template.Template, error) { return tmpl, nil } -func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) { +func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) error { out := c.Output if out == nil { out = io.Discard } if !c.Format.IsTable() { - _, _ = c.buffer.WriteTo(out) - return + _, err := c.buffer.WriteTo(out) + return err } - // Write column-headers and rows to the tab-writer buffer, then flush the output. + // A custom template can render rows successfully even when it cannot render + // all column headers. Buffer any headers it produces before writing the table. + var headers bytes.Buffer + _ = tmpl.Funcs(templates.HeaderFunctions).Execute(&headers, subContext.FullHeader()) + headers.WriteByte('\n') + 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() + if _, err := headers.WriteTo(tw); err != nil { + return err + } + if _, err := c.buffer.WriteTo(tw); err != nil { + return err + } + return tw.Flush() } func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error { @@ -137,6 +145,5 @@ func (c *Context) Write(sub SubContext, f SubFormat) error { return err } - c.postFormat(tmpl, sub) - return nil + return c.postFormat(tmpl, sub) } diff --git a/cli/command/formatter/formatter_test.go b/cli/command/formatter/formatter_test.go index 0c1675417de8..0af39375b42d 100644 --- a/cli/command/formatter/formatter_test.go +++ b/cli/command/formatter/formatter_test.go @@ -5,6 +5,7 @@ package formatter import ( "bytes" + "errors" "testing" "gotest.tools/v3/assert" @@ -124,3 +125,42 @@ test }) } } + +type failingOutput struct { + err error +} + +func (w failingOutput) Write([]byte) (int, error) { + return 0, w.err +} + +func TestContextWriteReturnsOutputError(t *testing.T) { + writeErr := errors.New("output write failed") + for _, format := range []Format{JSONFormatKey, "{{.Name}}", "table {{.Name}}", "table {{.Name}}\t{{.Name}}"} { + t.Run(string(format), func(t *testing.T) { + sub := fakeSubContext{Name: "test"} + ctx := Context{ + Format: format, + Output: failingOutput{writeErr}, + } + err := ctx.Write(sub, func(write func(SubContext) error) error { + return write(sub) + }) + assert.ErrorIs(t, err, writeErr) + }) + } +} + +func TestContextWriteKeepsPartialTableHeader(t *testing.T) { + sub := fakeSubContext{Name: "test-container"} + var output bytes.Buffer + ctx := Context{ + Format: "table {{.Name}} {{slice .Name 0 5}}", + Output: &output, + } + err := ctx.Write(sub, func(write func(SubContext) error) error { + return write(sub) + }) + assert.NilError(t, err) + assert.Equal(t, output.String(), "NAME \ntest-container test-\n") +}