Skip to content

Commit e6bf6dc

Browse files
committed
cli/command/formatter: minor cleanups
no need to initialize with an empty string Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 43e496b commit e6bf6dc

File tree

4 files changed

+12
-20
lines changed

4 files changed

+12
-20
lines changed

cli/command/formatter/container.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@ ports: {{- pad .Ports 1 0}}
6868

6969
// ContainerWrite renders the context for a list of containers
7070
func ContainerWrite(ctx Context, containers []container.Summary) error {
71-
render := func(format func(subContext SubContext) error) error {
71+
return ctx.Write(NewContainerContext(), func(format func(subContext SubContext) error) error {
7272
for _, ctr := range containers {
73-
err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr})
74-
if err != nil {
73+
if err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr}); err != nil {
7574
return err
7675
}
7776
}
7877
return nil
79-
}
80-
return ctx.Write(NewContainerContext(), render)
78+
})
8179
}
8280

8381
// ContainerContext is a struct used for rendering a list of containers in a Go template.

cli/command/formatter/container_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,62 +371,53 @@ size: 0B
371371
}
372372

373373
func TestContainerContextWriteWithNoContainers(t *testing.T) {
374-
out := bytes.NewBufferString("")
375-
containers := []container.Summary{}
376-
377374
cases := []struct {
378375
context Context
379376
expected string
380377
}{
381378
{
382379
context: Context{
383380
Format: "{{.Image}}",
384-
Output: out,
385381
},
386382
},
387383
{
388384
context: Context{
389385
Format: "table {{.Image}}",
390-
Output: out,
391386
},
392387
expected: "IMAGE\n",
393388
},
394389
{
395390
context: Context{
396391
Format: NewContainerFormat("{{.Image}}", false, true),
397-
Output: out,
398392
},
399393
},
400394
{
401395
context: Context{
402396
Format: NewContainerFormat("table {{.Image}}", false, true),
403-
Output: out,
404397
},
405398
expected: "IMAGE\n",
406399
},
407400
{
408401
context: Context{
409402
Format: "table {{.Image}}\t{{.Size}}",
410-
Output: out,
411403
},
412404
expected: "IMAGE SIZE\n",
413405
},
414406
{
415407
context: Context{
416408
Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
417-
Output: out,
418409
},
419410
expected: "IMAGE SIZE\n",
420411
},
421412
}
422413

423414
for _, tc := range cases {
424415
t.Run(string(tc.context.Format), func(t *testing.T) {
425-
err := ContainerWrite(tc.context, containers)
416+
out := new(bytes.Buffer)
417+
tc.context.Output = out
418+
err := ContainerWrite(tc.context, nil)
426419
assert.NilError(t, err)
427420
assert.Equal(t, out.String(), tc.expected)
428-
// Clean buffer
429-
out.Reset()
430421
})
431422
}
432423
}

cli/command/formatter/disk_usage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type DiskUsageContext struct {
4343
}
4444

4545
func (ctx *DiskUsageContext) startSubsection(format string) (*template.Template, error) {
46-
ctx.buffer = bytes.NewBufferString("")
46+
ctx.buffer = &bytes.Buffer{}
4747
ctx.header = ""
4848
ctx.Format = Format(format)
4949
ctx.preFormat()
@@ -87,7 +87,7 @@ func (ctx *DiskUsageContext) Write() (err error) {
8787
if ctx.Verbose {
8888
return ctx.verboseWrite()
8989
}
90-
ctx.buffer = bytes.NewBufferString("")
90+
ctx.buffer = &bytes.Buffer{}
9191
ctx.preFormat()
9292

9393
tmpl, err := ctx.parseFormat()

cli/command/formatter/formatter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ 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
87+
}
8588
if c.Format.IsTable() {
8689
t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0)
8790
buffer := bytes.NewBufferString("")
@@ -111,7 +114,7 @@ type SubFormat func(func(SubContext) error) error
111114

112115
// Write the template to the buffer using this Context
113116
func (c *Context) Write(sub SubContext, f SubFormat) error {
114-
c.buffer = bytes.NewBufferString("")
117+
c.buffer = &bytes.Buffer{}
115118
c.preFormat()
116119

117120
tmpl, err := c.parseFormat()

0 commit comments

Comments
 (0)