diff --git a/daemon/logger/awslogs/cloudwatchlogs.go b/daemon/logger/awslogs/cloudwatchlogs.go index cabfcbd8d3..3a7f2f631d 100644 --- a/daemon/logger/awslogs/cloudwatchlogs.go +++ b/daemon/logger/awslogs/cloudwatchlogs.go @@ -2,7 +2,6 @@ package awslogs import ( - "bytes" "fmt" "os" "regexp" @@ -22,7 +21,6 @@ import ( "github.com/docker/docker/daemon/logger" "github.com/docker/docker/daemon/logger/loggerutils" "github.com/docker/docker/dockerversion" - "github.com/docker/docker/pkg/templates" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -194,19 +192,6 @@ var strftimeToRegex = map[string]string{ /*milliseconds */ `%L`: `\.\d{3}`, } -func parseLogGroup(info logger.Info, groupTemplate string) (string, error) { - tmpl, err := templates.NewParse("log-group", groupTemplate) - if err != nil { - return "", err - } - buf := new(bytes.Buffer) - if err := tmpl.Execute(buf, &info); err != nil { - return "", err - } - - return buf.String(), nil -} - // newRegionFinder is a variable such that the implementation // can be swapped out for unit tests. var newRegionFinder = func() regionFinder { diff --git a/daemon/logger/loggerutils/log_tag.go b/daemon/logger/loggerutils/log_tag.go index f801047c41..7c170909a7 100644 --- a/daemon/logger/loggerutils/log_tag.go +++ b/daemon/logger/loggerutils/log_tag.go @@ -4,7 +4,7 @@ import ( "bytes" "github.com/docker/docker/daemon/logger" - "github.com/docker/docker/pkg/templates" + "github.com/docker/docker/daemon/logger/templates" ) // DefaultTemplate defines the defaults template logger should use. diff --git a/pkg/templates/templates.go b/daemon/logger/templates/templates.go similarity index 65% rename from pkg/templates/templates.go rename to daemon/logger/templates/templates.go index d2d7e0c3d7..f632e6e4b5 100644 --- a/pkg/templates/templates.go +++ b/daemon/logger/templates/templates.go @@ -27,34 +27,6 @@ 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 irrelevant 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) { - return NewParse("", format) -} - // NewParse creates a new tagged template with the basic functions // and parses the given format. func NewParse(tag, format string) (*template.Template, error) { diff --git a/daemon/logger/templates/templates_test.go b/daemon/logger/templates/templates_test.go new file mode 100644 index 0000000000..6205e1dc71 --- /dev/null +++ b/daemon/logger/templates/templates_test.go @@ -0,0 +1,18 @@ +package templates + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +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()) +} diff --git a/pkg/templates/templates_test.go b/pkg/templates/templates_test.go deleted file mode 100644 index 296bcb7107..0000000000 --- a/pkg/templates/templates_test.go +++ /dev/null @@ -1,88 +0,0 @@ -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()) - }) - } -} diff --git a/profiles/apparmor/apparmor.go b/profiles/apparmor/apparmor.go index 48b41c5b2d..e957727d52 100644 --- a/profiles/apparmor/apparmor.go +++ b/profiles/apparmor/apparmor.go @@ -9,9 +9,9 @@ import ( "os" "path" "strings" + "text/template" "github.com/docker/docker/pkg/aaparser" - "github.com/docker/docker/pkg/templates" ) var ( @@ -33,7 +33,7 @@ type profileData struct { // generateDefault creates an apparmor profile from ProfileData. func (p *profileData) generateDefault(out io.Writer) error { - compiled, err := templates.NewParse("apparmor_profile", baseTemplate) + compiled, err := template.New("apparmor_profile").Parse(baseTemplate) if err != nil { return err }