mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Ignore some functions in the Go template when header is rendered
This fix ignore some functions in the Go template when header is redendered, so that `--format "{{truncate .ID 1}}"` will still be able to redener the header correctly. Additional test cases have been added to the unit test. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
ea61dac9e6
commit
8b165cad1a
5 changed files with 31 additions and 24 deletions
|
@ -226,6 +226,14 @@ size: 0B
|
||||||
Context{Format: NewContainerFormat("{{.Image}}", false, true)},
|
Context{Format: NewContainerFormat("{{.Image}}", false, true)},
|
||||||
"ubuntu\nubuntu\n",
|
"ubuntu\nubuntu\n",
|
||||||
},
|
},
|
||||||
|
// Special headers for customerized table format
|
||||||
|
{
|
||||||
|
Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)},
|
||||||
|
`CONTAINER ID IMAGE CREATED/STATUS/ PORTS .NAMES STATUS
|
||||||
|
conta "ubuntu" 24 hours ago//.FOOBAR_BAZ
|
||||||
|
conta "ubuntu" 24 hours ago//.FOOBAR_BAR
|
||||||
|
`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testcase := range cases {
|
for _, testcase := range cases {
|
||||||
|
|
|
@ -2,13 +2,8 @@ package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
//"encoding/json"
|
|
||||||
//"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
//"time"
|
|
||||||
|
|
||||||
//"github.com/docker/docker/api/types"
|
|
||||||
//"github.com/docker/docker/pkg/stringid"
|
|
||||||
"github.com/docker/docker/pkg/testutil/assert"
|
"github.com/docker/docker/pkg/testutil/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -44,10 +39,6 @@ VOLUME NAME LINKS SIZE
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testcase := range cases {
|
for _, testcase := range cases {
|
||||||
//networks := []types.NetworkResource{
|
|
||||||
// {ID: "networkID1", Name: "foobar_baz", Driver: "foo", Scope: "local", Created: timestamp1},
|
|
||||||
// {ID: "networkID2", Name: "foobar_bar", Driver: "bar", Scope: "local", Created: timestamp2},
|
|
||||||
//}
|
|
||||||
out := bytes.NewBufferString("")
|
out := bytes.NewBufferString("")
|
||||||
testcase.context.Output = out
|
testcase.context.Output = out
|
||||||
testcase.context.Write()
|
testcase.context.Write()
|
||||||
|
|
|
@ -73,7 +73,7 @@ func (c *Context) postFormat(tmpl *template.Template, subContext subContext) {
|
||||||
if c.Format.IsTable() {
|
if c.Format.IsTable() {
|
||||||
t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
|
t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0)
|
||||||
buffer := bytes.NewBufferString("")
|
buffer := bytes.NewBufferString("")
|
||||||
tmpl.Execute(buffer, subContext.FullHeader())
|
tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader())
|
||||||
buffer.WriteTo(t)
|
buffer.WriteTo(t)
|
||||||
t.Write([]byte("\n"))
|
t.Write([]byte("\n"))
|
||||||
c.buffer.WriteTo(t)
|
c.buffer.WriteTo(t)
|
||||||
|
|
|
@ -76,20 +76,6 @@ func ImageWrite(ctx ImageContext, images []types.ImageSummary) error {
|
||||||
render := func(format func(subContext subContext) error) error {
|
render := func(format func(subContext subContext) error) error {
|
||||||
return imageFormat(ctx, images, format)
|
return imageFormat(ctx, images, format)
|
||||||
}
|
}
|
||||||
imageCtx := imageContext{}
|
|
||||||
imageCtx.header = map[string]string{
|
|
||||||
"ID": imageIDHeader,
|
|
||||||
"Repository": repositoryHeader,
|
|
||||||
"Tag": tagHeader,
|
|
||||||
"Digest": digestHeader,
|
|
||||||
"CreatedSince": createdSinceHeader,
|
|
||||||
"CreatedAt": createdAtHeader,
|
|
||||||
"Size": sizeHeader,
|
|
||||||
"Containers": containersHeader,
|
|
||||||
"VirtualSize": sizeHeader,
|
|
||||||
"SharedSize": sharedSizeHeader,
|
|
||||||
"UniqueSize": uniqueSizeHeader,
|
|
||||||
}
|
|
||||||
return ctx.Write(newImageContext(), render)
|
return ctx.Write(newImageContext(), render)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,28 @@ var basicFunctions = template.FuncMap{
|
||||||
"truncate": truncateWithLength,
|
"truncate": truncateWithLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HeaderFunctions are used to created headers of a table.
|
||||||
|
// This is a replacement of basicFunctions for header generation
|
||||||
|
// because we want the header to remain intact.
|
||||||
|
// Some functions like `split` are irrevelant so not added.
|
||||||
|
var HeaderFunctions = template.FuncMap{
|
||||||
|
"json": func(v string) string {
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
"title": func(v string) string {
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
"lower": func(v string) string {
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
"upper": func(v string) string {
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
"truncate": func(v string, l int) string {
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Parse creates a new anonymous template with the basic functions
|
// Parse creates a new anonymous template with the basic functions
|
||||||
// and parses the given format.
|
// and parses the given format.
|
||||||
func Parse(format string) (*template.Template, error) {
|
func Parse(format string) (*template.Template, error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue