From 8b165cad1aa8ce00ecc293ac0be82dd01392e548 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Fri, 3 Feb 2017 20:23:00 -0800 Subject: [PATCH] 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 --- cli/command/formatter/container_test.go | 8 ++++++++ cli/command/formatter/disk_usage_test.go | 9 --------- cli/command/formatter/formatter.go | 2 +- cli/command/formatter/image.go | 14 -------------- pkg/templates/templates.go | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index ef6e86c597..a5615d1768 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -226,6 +226,14 @@ size: 0B Context{Format: NewContainerFormat("{{.Image}}", false, true)}, "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 { diff --git a/cli/command/formatter/disk_usage_test.go b/cli/command/formatter/disk_usage_test.go index 06d1c2c1fe..318e1692be 100644 --- a/cli/command/formatter/disk_usage_test.go +++ b/cli/command/formatter/disk_usage_test.go @@ -2,13 +2,8 @@ package formatter import ( "bytes" - //"encoding/json" - //"strings" "testing" - //"time" - //"github.com/docker/docker/api/types" - //"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/testutil/assert" ) @@ -44,10 +39,6 @@ VOLUME NAME LINKS SIZE } 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("") testcase.context.Output = out testcase.context.Write() diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index 16e8e6af2c..a151e9c283 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -73,7 +73,7 @@ func (c *Context) postFormat(tmpl *template.Template, subContext subContext) { if c.Format.IsTable() { t := tabwriter.NewWriter(c.Output, 20, 1, 3, ' ', 0) buffer := bytes.NewBufferString("") - tmpl.Execute(buffer, subContext.FullHeader()) + tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader()) buffer.WriteTo(t) t.Write([]byte("\n")) c.buffer.WriteTo(t) diff --git a/cli/command/formatter/image.go b/cli/command/formatter/image.go index 8f18045c11..3aae34ea11 100644 --- a/cli/command/formatter/image.go +++ b/cli/command/formatter/image.go @@ -76,20 +76,6 @@ func ImageWrite(ctx ImageContext, images []types.ImageSummary) error { render := func(format func(subContext subContext) error) error { 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) } diff --git a/pkg/templates/templates.go b/pkg/templates/templates.go index 7988794316..2ac44fad44 100644 --- a/pkg/templates/templates.go +++ b/pkg/templates/templates.go @@ -22,6 +22,28 @@ var basicFunctions = template.FuncMap{ "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 // and parses the given format. func Parse(format string) (*template.Template, error) {