diff --git a/components/cli/cli/command/container/list.go b/components/cli/cli/command/container/list.go index 844ef57519..ec1ad69e13 100644 --- a/components/cli/cli/command/container/list.go +++ b/components/cli/cli/command/container/list.go @@ -7,8 +7,8 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/opts" + "github.com/docker/cli/templates" "github.com/docker/docker/api/types" - "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" "golang.org/x/net/context" ) diff --git a/components/cli/cli/command/formatter/formatter.go b/components/cli/cli/command/formatter/formatter.go index 3f07aee963..c63e1a4908 100644 --- a/components/cli/cli/command/formatter/formatter.go +++ b/components/cli/cli/command/formatter/formatter.go @@ -7,7 +7,7 @@ import ( "text/tabwriter" "text/template" - "github.com/docker/docker/pkg/templates" + "github.com/docker/cli/templates" "github.com/pkg/errors" ) diff --git a/components/cli/cli/command/inspect/inspector.go b/components/cli/cli/command/inspect/inspector.go index 054381f50f..b03dbed264 100644 --- a/components/cli/cli/command/inspect/inspector.go +++ b/components/cli/cli/command/inspect/inspector.go @@ -9,7 +9,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/cli/cli" - "github.com/docker/docker/pkg/templates" + "github.com/docker/cli/templates" "github.com/pkg/errors" ) diff --git a/components/cli/cli/command/inspect/inspector_test.go b/components/cli/cli/command/inspect/inspector_test.go index 721bcf84c5..7d19fceda2 100644 --- a/components/cli/cli/command/inspect/inspector_test.go +++ b/components/cli/cli/command/inspect/inspector_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/docker/docker/pkg/templates" + "github.com/docker/cli/templates" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/components/cli/cli/command/system/events.go b/components/cli/cli/command/system/events.go index ba83d26fa3..6dbc51e2bc 100644 --- a/components/cli/cli/command/system/events.go +++ b/components/cli/cli/command/system/events.go @@ -12,10 +12,10 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/opts" + "github.com/docker/cli/templates" "github.com/docker/docker/api/types" eventtypes "github.com/docker/docker/api/types/events" "github.com/docker/docker/pkg/jsonlog" - "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" "golang.org/x/net/context" ) diff --git a/components/cli/cli/command/system/info.go b/components/cli/cli/command/system/info.go index f2a404e905..73f1765435 100644 --- a/components/cli/cli/command/system/info.go +++ b/components/cli/cli/command/system/info.go @@ -9,9 +9,9 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/debug" + "github.com/docker/cli/templates" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" - "github.com/docker/docker/pkg/templates" "github.com/docker/go-units" "github.com/spf13/cobra" "golang.org/x/net/context" diff --git a/components/cli/cli/command/system/version.go b/components/cli/cli/command/system/version.go index 21c5a6df30..9cca599fe3 100644 --- a/components/cli/cli/command/system/version.go +++ b/components/cli/cli/command/system/version.go @@ -8,8 +8,8 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/templates" "github.com/docker/docker/api/types" - "github.com/docker/docker/pkg/templates" "github.com/spf13/cobra" ) diff --git a/components/cli/vendor/github.com/docker/docker/pkg/templates/templates.go b/components/cli/templates/templates.go similarity index 97% rename from components/cli/vendor/github.com/docker/docker/pkg/templates/templates.go rename to components/cli/templates/templates.go index d2d7e0c3d7..80cab5ed34 100644 --- a/components/cli/vendor/github.com/docker/docker/pkg/templates/templates.go +++ b/components/cli/templates/templates.go @@ -44,7 +44,7 @@ var HeaderFunctions = template.FuncMap{ "upper": func(v string) string { return v }, - "truncate": func(v string, l int) string { + "truncate": func(v string, _ int) string { return v }, } diff --git a/components/cli/templates/templates_test.go b/components/cli/templates/templates_test.go new file mode 100644 index 0000000000..296bcb7107 --- /dev/null +++ b/components/cli/templates/templates_test.go @@ -0,0 +1,88 @@ +package templates + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +// Github #32120 +func TestParseJSONFunctions(t *testing.T) { + tm, err := Parse(`{{json .Ports}}`) + assert.NoError(t, err) + + var b bytes.Buffer + assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"})) + want := "\"0.0.0.0:2->8/udp\"" + assert.Equal(t, want, b.String()) +} + +func TestParseStringFunctions(t *testing.T) { + tm, err := Parse(`{{join (split . ":") "/"}}`) + assert.NoError(t, err) + + var b bytes.Buffer + assert.NoError(t, tm.Execute(&b, "text:with:colon")) + want := "text/with/colon" + assert.Equal(t, want, b.String()) +} + +func TestNewParse(t *testing.T) { + tm, err := NewParse("foo", "this is a {{ . }}") + assert.NoError(t, err) + + var b bytes.Buffer + assert.NoError(t, tm.Execute(&b, "string")) + want := "this is a string" + assert.Equal(t, want, b.String()) +} + +func TestParseTruncateFunction(t *testing.T) { + source := "tupx5xzf6hvsrhnruz5cr8gwp" + + testCases := []struct { + template string + expected string + }{ + { + template: `{{truncate . 5}}`, + expected: "tupx5", + }, + { + template: `{{truncate . 25}}`, + expected: "tupx5xzf6hvsrhnruz5cr8gwp", + }, + { + template: `{{truncate . 30}}`, + expected: "tupx5xzf6hvsrhnruz5cr8gwp", + }, + { + template: `{{pad . 3 3}}`, + expected: " tupx5xzf6hvsrhnruz5cr8gwp ", + }, + } + + for _, testCase := range testCases { + tm, err := Parse(testCase.template) + assert.NoError(t, err) + + t.Run("Non Empty Source Test with template: "+testCase.template, func(t *testing.T) { + var b bytes.Buffer + assert.NoError(t, tm.Execute(&b, source)) + assert.Equal(t, testCase.expected, b.String()) + }) + + t.Run("Empty Source Test with template: "+testCase.template, func(t *testing.T) { + var c bytes.Buffer + assert.NoError(t, tm.Execute(&c, "")) + assert.Equal(t, "", c.String()) + }) + + t.Run("Nil Source Test with template: "+testCase.template, func(t *testing.T) { + var c bytes.Buffer + assert.Error(t, tm.Execute(&c, nil)) + assert.Equal(t, "", c.String()) + }) + } +}