- Remove unused function and variables from the package
- Remove usage of it from `profiles/apparmor` where it wasn't required
- Move the package to `daemon/logger/templates` where it's only used
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -2,7 +2,6 @@ |
| 2 | 2 |
package awslogs |
| 3 | 3 |
|
| 4 | 4 |
import ( |
| 5 |
- "bytes" |
|
| 6 | 5 |
"fmt" |
| 7 | 6 |
"os" |
| 8 | 7 |
"regexp" |
| ... | ... |
@@ -22,7 +21,6 @@ import ( |
| 22 | 22 |
"github.com/docker/docker/daemon/logger" |
| 23 | 23 |
"github.com/docker/docker/daemon/logger/loggerutils" |
| 24 | 24 |
"github.com/docker/docker/dockerversion" |
| 25 |
- "github.com/docker/docker/pkg/templates" |
|
| 26 | 25 |
"github.com/pkg/errors" |
| 27 | 26 |
"github.com/sirupsen/logrus" |
| 28 | 27 |
) |
| ... | ... |
@@ -194,19 +192,6 @@ var strftimeToRegex = map[string]string{
|
| 194 | 194 |
/*milliseconds */ `%L`: `\.\d{3}`,
|
| 195 | 195 |
} |
| 196 | 196 |
|
| 197 |
-func parseLogGroup(info logger.Info, groupTemplate string) (string, error) {
|
|
| 198 |
- tmpl, err := templates.NewParse("log-group", groupTemplate)
|
|
| 199 |
- if err != nil {
|
|
| 200 |
- return "", err |
|
| 201 |
- } |
|
| 202 |
- buf := new(bytes.Buffer) |
|
| 203 |
- if err := tmpl.Execute(buf, &info); err != nil {
|
|
| 204 |
- return "", err |
|
| 205 |
- } |
|
| 206 |
- |
|
| 207 |
- return buf.String(), nil |
|
| 208 |
-} |
|
| 209 |
- |
|
| 210 | 197 |
// newRegionFinder is a variable such that the implementation |
| 211 | 198 |
// can be swapped out for unit tests. |
| 212 | 199 |
var newRegionFinder = func() regionFinder {
|
| 11 | 11 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,50 @@ |
| 0 |
+package templates |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "strings" |
|
| 6 |
+ "text/template" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+// basicFunctions are the set of initial |
|
| 10 |
+// functions provided to every template. |
|
| 11 |
+var basicFunctions = template.FuncMap{
|
|
| 12 |
+ "json": func(v interface{}) string {
|
|
| 13 |
+ buf := &bytes.Buffer{}
|
|
| 14 |
+ enc := json.NewEncoder(buf) |
|
| 15 |
+ enc.SetEscapeHTML(false) |
|
| 16 |
+ enc.Encode(v) |
|
| 17 |
+ // Remove the trailing new line added by the encoder |
|
| 18 |
+ return strings.TrimSpace(buf.String()) |
|
| 19 |
+ }, |
|
| 20 |
+ "split": strings.Split, |
|
| 21 |
+ "join": strings.Join, |
|
| 22 |
+ "title": strings.Title, |
|
| 23 |
+ "lower": strings.ToLower, |
|
| 24 |
+ "upper": strings.ToUpper, |
|
| 25 |
+ "pad": padWithSpace, |
|
| 26 |
+ "truncate": truncateWithLength, |
|
| 27 |
+} |
|
| 28 |
+ |
|
| 29 |
+// NewParse creates a new tagged template with the basic functions |
|
| 30 |
+// and parses the given format. |
|
| 31 |
+func NewParse(tag, format string) (*template.Template, error) {
|
|
| 32 |
+ return template.New(tag).Funcs(basicFunctions).Parse(format) |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+// padWithSpace adds whitespace to the input if the input is non-empty |
|
| 36 |
+func padWithSpace(source string, prefix, suffix int) string {
|
|
| 37 |
+ if source == "" {
|
|
| 38 |
+ return source |
|
| 39 |
+ } |
|
| 40 |
+ return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
|
|
| 41 |
+} |
|
| 42 |
+ |
|
| 43 |
+// truncateWithLength truncates the source string up to the length provided by the input |
|
| 44 |
+func truncateWithLength(source string, length int) string {
|
|
| 45 |
+ if len(source) < length {
|
|
| 46 |
+ return source |
|
| 47 |
+ } |
|
| 48 |
+ return source[:length] |
|
| 49 |
+} |
| 0 | 50 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,18 @@ |
| 0 |
+package templates |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "testing" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/stretchr/testify/assert" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+func TestNewParse(t *testing.T) {
|
|
| 10 |
+ tm, err := NewParse("foo", "this is a {{ . }}")
|
|
| 11 |
+ assert.NoError(t, err) |
|
| 12 |
+ |
|
| 13 |
+ var b bytes.Buffer |
|
| 14 |
+ assert.NoError(t, tm.Execute(&b, "string")) |
|
| 15 |
+ want := "this is a string" |
|
| 16 |
+ assert.Equal(t, want, b.String()) |
|
| 17 |
+} |
| 0 | 18 |
deleted file mode 100644 |
| ... | ... |
@@ -1,78 +0,0 @@ |
| 1 |
-package templates |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bytes" |
|
| 5 |
- "encoding/json" |
|
| 6 |
- "strings" |
|
| 7 |
- "text/template" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-// basicFunctions are the set of initial |
|
| 11 |
-// functions provided to every template. |
|
| 12 |
-var basicFunctions = template.FuncMap{
|
|
| 13 |
- "json": func(v interface{}) string {
|
|
| 14 |
- buf := &bytes.Buffer{}
|
|
| 15 |
- enc := json.NewEncoder(buf) |
|
| 16 |
- enc.SetEscapeHTML(false) |
|
| 17 |
- enc.Encode(v) |
|
| 18 |
- // Remove the trailing new line added by the encoder |
|
| 19 |
- return strings.TrimSpace(buf.String()) |
|
| 20 |
- }, |
|
| 21 |
- "split": strings.Split, |
|
| 22 |
- "join": strings.Join, |
|
| 23 |
- "title": strings.Title, |
|
| 24 |
- "lower": strings.ToLower, |
|
| 25 |
- "upper": strings.ToUpper, |
|
| 26 |
- "pad": padWithSpace, |
|
| 27 |
- "truncate": truncateWithLength, |
|
| 28 |
-} |
|
| 29 |
- |
|
| 30 |
-// HeaderFunctions are used to created headers of a table. |
|
| 31 |
-// This is a replacement of basicFunctions for header generation |
|
| 32 |
-// because we want the header to remain intact. |
|
| 33 |
-// Some functions like `split` are irrelevant so not added. |
|
| 34 |
-var HeaderFunctions = template.FuncMap{
|
|
| 35 |
- "json": func(v string) string {
|
|
| 36 |
- return v |
|
| 37 |
- }, |
|
| 38 |
- "title": func(v string) string {
|
|
| 39 |
- return v |
|
| 40 |
- }, |
|
| 41 |
- "lower": func(v string) string {
|
|
| 42 |
- return v |
|
| 43 |
- }, |
|
| 44 |
- "upper": func(v string) string {
|
|
| 45 |
- return v |
|
| 46 |
- }, |
|
| 47 |
- "truncate": func(v string, l int) string {
|
|
| 48 |
- return v |
|
| 49 |
- }, |
|
| 50 |
-} |
|
| 51 |
- |
|
| 52 |
-// Parse creates a new anonymous template with the basic functions |
|
| 53 |
-// and parses the given format. |
|
| 54 |
-func Parse(format string) (*template.Template, error) {
|
|
| 55 |
- return NewParse("", format)
|
|
| 56 |
-} |
|
| 57 |
- |
|
| 58 |
-// NewParse creates a new tagged template with the basic functions |
|
| 59 |
-// and parses the given format. |
|
| 60 |
-func NewParse(tag, format string) (*template.Template, error) {
|
|
| 61 |
- return template.New(tag).Funcs(basicFunctions).Parse(format) |
|
| 62 |
-} |
|
| 63 |
- |
|
| 64 |
-// padWithSpace adds whitespace to the input if the input is non-empty |
|
| 65 |
-func padWithSpace(source string, prefix, suffix int) string {
|
|
| 66 |
- if source == "" {
|
|
| 67 |
- return source |
|
| 68 |
- } |
|
| 69 |
- return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
|
|
| 70 |
-} |
|
| 71 |
- |
|
| 72 |
-// truncateWithLength truncates the source string up to the length provided by the input |
|
| 73 |
-func truncateWithLength(source string, length int) string {
|
|
| 74 |
- if len(source) < length {
|
|
| 75 |
- return source |
|
| 76 |
- } |
|
| 77 |
- return source[:length] |
|
| 78 |
-} |
| 79 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,88 +0,0 @@ |
| 1 |
-package templates |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bytes" |
|
| 5 |
- "testing" |
|
| 6 |
- |
|
| 7 |
- "github.com/stretchr/testify/assert" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-// Github #32120 |
|
| 11 |
-func TestParseJSONFunctions(t *testing.T) {
|
|
| 12 |
- tm, err := Parse(`{{json .Ports}}`)
|
|
| 13 |
- assert.NoError(t, err) |
|
| 14 |
- |
|
| 15 |
- var b bytes.Buffer |
|
| 16 |
- assert.NoError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
|
|
| 17 |
- want := "\"0.0.0.0:2->8/udp\"" |
|
| 18 |
- assert.Equal(t, want, b.String()) |
|
| 19 |
-} |
|
| 20 |
- |
|
| 21 |
-func TestParseStringFunctions(t *testing.T) {
|
|
| 22 |
- tm, err := Parse(`{{join (split . ":") "/"}}`)
|
|
| 23 |
- assert.NoError(t, err) |
|
| 24 |
- |
|
| 25 |
- var b bytes.Buffer |
|
| 26 |
- assert.NoError(t, tm.Execute(&b, "text:with:colon")) |
|
| 27 |
- want := "text/with/colon" |
|
| 28 |
- assert.Equal(t, want, b.String()) |
|
| 29 |
-} |
|
| 30 |
- |
|
| 31 |
-func TestNewParse(t *testing.T) {
|
|
| 32 |
- tm, err := NewParse("foo", "this is a {{ . }}")
|
|
| 33 |
- assert.NoError(t, err) |
|
| 34 |
- |
|
| 35 |
- var b bytes.Buffer |
|
| 36 |
- assert.NoError(t, tm.Execute(&b, "string")) |
|
| 37 |
- want := "this is a string" |
|
| 38 |
- assert.Equal(t, want, b.String()) |
|
| 39 |
-} |
|
| 40 |
- |
|
| 41 |
-func TestParseTruncateFunction(t *testing.T) {
|
|
| 42 |
- source := "tupx5xzf6hvsrhnruz5cr8gwp" |
|
| 43 |
- |
|
| 44 |
- testCases := []struct {
|
|
| 45 |
- template string |
|
| 46 |
- expected string |
|
| 47 |
- }{
|
|
| 48 |
- {
|
|
| 49 |
- template: `{{truncate . 5}}`,
|
|
| 50 |
- expected: "tupx5", |
|
| 51 |
- }, |
|
| 52 |
- {
|
|
| 53 |
- template: `{{truncate . 25}}`,
|
|
| 54 |
- expected: "tupx5xzf6hvsrhnruz5cr8gwp", |
|
| 55 |
- }, |
|
| 56 |
- {
|
|
| 57 |
- template: `{{truncate . 30}}`,
|
|
| 58 |
- expected: "tupx5xzf6hvsrhnruz5cr8gwp", |
|
| 59 |
- }, |
|
| 60 |
- {
|
|
| 61 |
- template: `{{pad . 3 3}}`,
|
|
| 62 |
- expected: " tupx5xzf6hvsrhnruz5cr8gwp ", |
|
| 63 |
- }, |
|
| 64 |
- } |
|
| 65 |
- |
|
| 66 |
- for _, testCase := range testCases {
|
|
| 67 |
- tm, err := Parse(testCase.template) |
|
| 68 |
- assert.NoError(t, err) |
|
| 69 |
- |
|
| 70 |
- t.Run("Non Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
| 71 |
- var b bytes.Buffer |
|
| 72 |
- assert.NoError(t, tm.Execute(&b, source)) |
|
| 73 |
- assert.Equal(t, testCase.expected, b.String()) |
|
| 74 |
- }) |
|
| 75 |
- |
|
| 76 |
- t.Run("Empty Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
| 77 |
- var c bytes.Buffer |
|
| 78 |
- assert.NoError(t, tm.Execute(&c, "")) |
|
| 79 |
- assert.Equal(t, "", c.String()) |
|
| 80 |
- }) |
|
| 81 |
- |
|
| 82 |
- t.Run("Nil Source Test with template: "+testCase.template, func(t *testing.T) {
|
|
| 83 |
- var c bytes.Buffer |
|
| 84 |
- assert.Error(t, tm.Execute(&c, nil)) |
|
| 85 |
- assert.Equal(t, "", c.String()) |
|
| 86 |
- }) |
|
| 87 |
- } |
|
| 88 |
-} |
| ... | ... |
@@ -9,9 +9,9 @@ import ( |
| 9 | 9 |
"os" |
| 10 | 10 |
"path" |
| 11 | 11 |
"strings" |
| 12 |
+ "text/template" |
|
| 12 | 13 |
|
| 13 | 14 |
"github.com/docker/docker/pkg/aaparser" |
| 14 |
- "github.com/docker/docker/pkg/templates" |
|
| 15 | 15 |
) |
| 16 | 16 |
|
| 17 | 17 |
var ( |
| ... | ... |
@@ -33,7 +33,7 @@ type profileData struct {
|
| 33 | 33 |
|
| 34 | 34 |
// generateDefault creates an apparmor profile from ProfileData. |
| 35 | 35 |
func (p *profileData) generateDefault(out io.Writer) error {
|
| 36 |
- compiled, err := templates.NewParse("apparmor_profile", baseTemplate)
|
|
| 36 |
+ compiled, err := template.New("apparmor_profile").Parse(baseTemplate)
|
|
| 37 | 37 |
if err != nil {
|
| 38 | 38 |
return err |
| 39 | 39 |
} |