From e6bf6dcd908d0a850f94a39156f58bc3af644a02 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 16 May 2025 13:39:08 +0200 Subject: [PATCH] cli/command/formatter: minor cleanups no need to initialize with an empty string Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/container.go | 8 +++----- cli/command/formatter/container_test.go | 15 +++------------ cli/command/formatter/disk_usage.go | 4 ++-- cli/command/formatter/formatter.go | 5 ++++- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index 4e5a1cd750..2badc9a398 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -68,16 +68,14 @@ ports: {{- pad .Ports 1 0}} // ContainerWrite renders the context for a list of containers func ContainerWrite(ctx Context, containers []container.Summary) error { - render := func(format func(subContext SubContext) error) error { + return ctx.Write(NewContainerContext(), func(format func(subContext SubContext) error) error { for _, ctr := range containers { - err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr}) - if err != nil { + if err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr}); err != nil { return err } } return nil - } - return ctx.Write(NewContainerContext(), render) + }) } // ContainerContext is a struct used for rendering a list of containers in a Go template. diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 126991222a..fe595484b0 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -371,9 +371,6 @@ size: 0B } func TestContainerContextWriteWithNoContainers(t *testing.T) { - out := bytes.NewBufferString("") - containers := []container.Summary{} - cases := []struct { context Context expected string @@ -381,40 +378,34 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) { { context: Context{ Format: "{{.Image}}", - Output: out, }, }, { context: Context{ Format: "table {{.Image}}", - Output: out, }, expected: "IMAGE\n", }, { context: Context{ Format: NewContainerFormat("{{.Image}}", false, true), - Output: out, }, }, { context: Context{ Format: NewContainerFormat("table {{.Image}}", false, true), - Output: out, }, expected: "IMAGE\n", }, { context: Context{ Format: "table {{.Image}}\t{{.Size}}", - Output: out, }, expected: "IMAGE SIZE\n", }, { context: Context{ Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true), - Output: out, }, expected: "IMAGE SIZE\n", }, @@ -422,11 +413,11 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) { for _, tc := range cases { t.Run(string(tc.context.Format), func(t *testing.T) { - err := ContainerWrite(tc.context, containers) + out := new(bytes.Buffer) + tc.context.Output = out + err := ContainerWrite(tc.context, nil) assert.NilError(t, err) assert.Equal(t, out.String(), tc.expected) - // Clean buffer - out.Reset() }) } } diff --git a/cli/command/formatter/disk_usage.go b/cli/command/formatter/disk_usage.go index f0fb4e1f2c..e815890ca2 100644 --- a/cli/command/formatter/disk_usage.go +++ b/cli/command/formatter/disk_usage.go @@ -43,7 +43,7 @@ type DiskUsageContext struct { } func (ctx *DiskUsageContext) startSubsection(format string) (*template.Template, error) { - ctx.buffer = bytes.NewBufferString("") + ctx.buffer = &bytes.Buffer{} ctx.header = "" ctx.Format = Format(format) ctx.preFormat() @@ -87,7 +87,7 @@ func (ctx *DiskUsageContext) Write() (err error) { if ctx.Verbose { return ctx.verboseWrite() } - ctx.buffer = bytes.NewBufferString("") + ctx.buffer = &bytes.Buffer{} ctx.preFormat() tmpl, err := ctx.parseFormat() diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index 760f77556d..7803cabe45 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -82,6 +82,9 @@ func (c *Context) parseFormat() (*template.Template, error) { } func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) { + if c.Output == nil { + c.Output = io.Discard + } if c.Format.IsTable() { t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0) buffer := bytes.NewBufferString("") @@ -111,7 +114,7 @@ type SubFormat func(func(SubContext) error) error // Write the template to the buffer using this Context func (c *Context) Write(sub SubContext, f SubFormat) error { - c.buffer = bytes.NewBufferString("") + c.buffer = &bytes.Buffer{} c.preFormat() tmpl, err := c.parseFormat()