Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions cli/command/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
40 changes: 40 additions & 0 deletions cli/command/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package formatter

import (
"bytes"
"errors"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -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")
}