cli/command/formatter: minor cleanups
no need to initialize with an empty string Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
43e496b396
commit
e6bf6dcd90
@ -68,16 +68,14 @@ ports: {{- pad .Ports 1 0}}
|
|||||||
|
|
||||||
// ContainerWrite renders the context for a list of containers
|
// ContainerWrite renders the context for a list of containers
|
||||||
func ContainerWrite(ctx Context, containers []container.Summary) error {
|
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 {
|
for _, ctr := range containers {
|
||||||
err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr})
|
if err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr}); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
})
|
||||||
return ctx.Write(NewContainerContext(), render)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainerContext is a struct used for rendering a list of containers in a Go template.
|
// ContainerContext is a struct used for rendering a list of containers in a Go template.
|
||||||
|
@ -371,9 +371,6 @@ size: 0B
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
||||||
out := bytes.NewBufferString("")
|
|
||||||
containers := []container.Summary{}
|
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
context Context
|
context Context
|
||||||
expected string
|
expected string
|
||||||
@ -381,40 +378,34 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
|||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: "{{.Image}}",
|
Format: "{{.Image}}",
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: "table {{.Image}}",
|
Format: "table {{.Image}}",
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
expected: "IMAGE\n",
|
expected: "IMAGE\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: NewContainerFormat("{{.Image}}", false, true),
|
Format: NewContainerFormat("{{.Image}}", false, true),
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: NewContainerFormat("table {{.Image}}", false, true),
|
Format: NewContainerFormat("table {{.Image}}", false, true),
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
expected: "IMAGE\n",
|
expected: "IMAGE\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: "table {{.Image}}\t{{.Size}}",
|
Format: "table {{.Image}}\t{{.Size}}",
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
expected: "IMAGE SIZE\n",
|
expected: "IMAGE SIZE\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
context: Context{
|
context: Context{
|
||||||
Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
|
Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
|
||||||
Output: out,
|
|
||||||
},
|
},
|
||||||
expected: "IMAGE SIZE\n",
|
expected: "IMAGE SIZE\n",
|
||||||
},
|
},
|
||||||
@ -422,11 +413,11 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
t.Run(string(tc.context.Format), func(t *testing.T) {
|
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.NilError(t, err)
|
||||||
assert.Equal(t, out.String(), tc.expected)
|
assert.Equal(t, out.String(), tc.expected)
|
||||||
// Clean buffer
|
|
||||||
out.Reset()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ type DiskUsageContext struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *DiskUsageContext) startSubsection(format string) (*template.Template, error) {
|
func (ctx *DiskUsageContext) startSubsection(format string) (*template.Template, error) {
|
||||||
ctx.buffer = bytes.NewBufferString("")
|
ctx.buffer = &bytes.Buffer{}
|
||||||
ctx.header = ""
|
ctx.header = ""
|
||||||
ctx.Format = Format(format)
|
ctx.Format = Format(format)
|
||||||
ctx.preFormat()
|
ctx.preFormat()
|
||||||
@ -87,7 +87,7 @@ func (ctx *DiskUsageContext) Write() (err error) {
|
|||||||
if ctx.Verbose {
|
if ctx.Verbose {
|
||||||
return ctx.verboseWrite()
|
return ctx.verboseWrite()
|
||||||
}
|
}
|
||||||
ctx.buffer = bytes.NewBufferString("")
|
ctx.buffer = &bytes.Buffer{}
|
||||||
ctx.preFormat()
|
ctx.preFormat()
|
||||||
|
|
||||||
tmpl, err := ctx.parseFormat()
|
tmpl, err := ctx.parseFormat()
|
||||||
|
@ -82,6 +82,9 @@ func (c *Context) parseFormat() (*template.Template, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {
|
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {
|
||||||
|
if c.Output == nil {
|
||||||
|
c.Output = io.Discard
|
||||||
|
}
|
||||||
if c.Format.IsTable() {
|
if c.Format.IsTable() {
|
||||||
t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0)
|
t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0)
|
||||||
buffer := bytes.NewBufferString("")
|
buffer := bytes.NewBufferString("")
|
||||||
@ -111,7 +114,7 @@ type SubFormat func(func(SubContext) error) error
|
|||||||
|
|
||||||
// Write the template to the buffer using this Context
|
// Write the template to the buffer using this Context
|
||||||
func (c *Context) Write(sub SubContext, f SubFormat) error {
|
func (c *Context) Write(sub SubContext, f SubFormat) error {
|
||||||
c.buffer = bytes.NewBufferString("")
|
c.buffer = &bytes.Buffer{}
|
||||||
c.preFormat()
|
c.preFormat()
|
||||||
|
|
||||||
tmpl, err := c.parseFormat()
|
tmpl, err := c.parseFormat()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user