Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -10,7 +10,7 @@ import ( |
| 10 | 10 |
"github.com/docker/docker/cli/command" |
| 11 | 11 |
"github.com/docker/docker/cli/command/formatter" |
| 12 | 12 |
"github.com/docker/docker/opts" |
| 13 |
- "github.com/docker/docker/utils/templates" |
|
| 13 |
+ "github.com/docker/docker/pkg/templates" |
|
| 14 | 14 |
"github.com/spf13/cobra" |
| 15 | 15 |
) |
| 16 | 16 |
|
| ... | ... |
@@ -17,7 +17,7 @@ import ( |
| 17 | 17 |
"github.com/docker/docker/cli/command" |
| 18 | 18 |
"github.com/docker/docker/opts" |
| 19 | 19 |
"github.com/docker/docker/pkg/jsonlog" |
| 20 |
- "github.com/docker/docker/utils/templates" |
|
| 20 |
+ "github.com/docker/docker/pkg/templates" |
|
| 21 | 21 |
"github.com/spf13/cobra" |
| 22 | 22 |
) |
| 23 | 23 |
|
| ... | ... |
@@ -14,7 +14,7 @@ import ( |
| 14 | 14 |
"github.com/docker/docker/cli/command" |
| 15 | 15 |
"github.com/docker/docker/cli/debug" |
| 16 | 16 |
"github.com/docker/docker/pkg/ioutils" |
| 17 |
- "github.com/docker/docker/utils/templates" |
|
| 17 |
+ "github.com/docker/docker/pkg/templates" |
|
| 18 | 18 |
"github.com/docker/go-units" |
| 19 | 19 |
"github.com/spf13/cobra" |
| 20 | 20 |
) |
| ... | ... |
@@ -11,7 +11,7 @@ import ( |
| 11 | 11 |
"github.com/docker/docker/cli" |
| 12 | 12 |
"github.com/docker/docker/cli/command" |
| 13 | 13 |
"github.com/docker/docker/dockerversion" |
| 14 |
- "github.com/docker/docker/utils/templates" |
|
| 14 |
+ "github.com/docker/docker/pkg/templates" |
|
| 15 | 15 |
"github.com/spf13/cobra" |
| 16 | 16 |
) |
| 17 | 17 |
|
| 11 | 11 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,42 @@ |
| 0 |
+package templates |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "encoding/json" |
|
| 4 |
+ "strings" |
|
| 5 |
+ "text/template" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+// basicFunctions are the set of initial |
|
| 9 |
+// functions provided to every template. |
|
| 10 |
+var basicFunctions = template.FuncMap{
|
|
| 11 |
+ "json": func(v interface{}) string {
|
|
| 12 |
+ a, _ := json.Marshal(v) |
|
| 13 |
+ return string(a) |
|
| 14 |
+ }, |
|
| 15 |
+ "split": strings.Split, |
|
| 16 |
+ "join": strings.Join, |
|
| 17 |
+ "title": strings.Title, |
|
| 18 |
+ "lower": strings.ToLower, |
|
| 19 |
+ "upper": strings.ToUpper, |
|
| 20 |
+ "pad": padWithSpace, |
|
| 21 |
+} |
|
| 22 |
+ |
|
| 23 |
+// Parse creates a new annonymous template with the basic functions |
|
| 24 |
+// and parses the given format. |
|
| 25 |
+func Parse(format string) (*template.Template, error) {
|
|
| 26 |
+ return NewParse("", format)
|
|
| 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 |
+} |
| 0 | 42 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,38 @@ |
| 0 |
+package templates |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "testing" |
|
| 5 |
+) |
|
| 6 |
+ |
|
| 7 |
+func TestParseStringFunctions(t *testing.T) {
|
|
| 8 |
+ tm, err := Parse(`{{join (split . ":") "/"}}`)
|
|
| 9 |
+ if err != nil {
|
|
| 10 |
+ t.Fatal(err) |
|
| 11 |
+ } |
|
| 12 |
+ |
|
| 13 |
+ var b bytes.Buffer |
|
| 14 |
+ if err := tm.Execute(&b, "text:with:colon"); err != nil {
|
|
| 15 |
+ t.Fatal(err) |
|
| 16 |
+ } |
|
| 17 |
+ want := "text/with/colon" |
|
| 18 |
+ if b.String() != want {
|
|
| 19 |
+ t.Fatalf("expected %s, got %s", want, b.String())
|
|
| 20 |
+ } |
|
| 21 |
+} |
|
| 22 |
+ |
|
| 23 |
+func TestNewParse(t *testing.T) {
|
|
| 24 |
+ tm, err := NewParse("foo", "this is a {{ . }}")
|
|
| 25 |
+ if err != nil {
|
|
| 26 |
+ t.Fatal(err) |
|
| 27 |
+ } |
|
| 28 |
+ |
|
| 29 |
+ var b bytes.Buffer |
|
| 30 |
+ if err := tm.Execute(&b, "string"); err != nil {
|
|
| 31 |
+ t.Fatal(err) |
|
| 32 |
+ } |
|
| 33 |
+ want := "this is a string" |
|
| 34 |
+ if b.String() != want {
|
|
| 35 |
+ t.Fatalf("expected %s, got %s", want, b.String())
|
|
| 36 |
+ } |
|
| 37 |
+} |
| 18 | 18 |
deleted file mode 100644 |
| ... | ... |
@@ -1,42 +0,0 @@ |
| 1 |
-package templates |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 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 |
- a, _ := json.Marshal(v) |
|
| 14 |
- return string(a) |
|
| 15 |
- }, |
|
| 16 |
- "split": strings.Split, |
|
| 17 |
- "join": strings.Join, |
|
| 18 |
- "title": strings.Title, |
|
| 19 |
- "lower": strings.ToLower, |
|
| 20 |
- "upper": strings.ToUpper, |
|
| 21 |
- "pad": padWithSpace, |
|
| 22 |
-} |
|
| 23 |
- |
|
| 24 |
-// Parse creates a new annonymous template with the basic functions |
|
| 25 |
-// and parses the given format. |
|
| 26 |
-func Parse(format string) (*template.Template, error) {
|
|
| 27 |
- return NewParse("", format)
|
|
| 28 |
-} |
|
| 29 |
- |
|
| 30 |
-// NewParse creates a new tagged template with the basic functions |
|
| 31 |
-// and parses the given format. |
|
| 32 |
-func NewParse(tag, format string) (*template.Template, error) {
|
|
| 33 |
- return template.New(tag).Funcs(basicFunctions).Parse(format) |
|
| 34 |
-} |
|
| 35 |
- |
|
| 36 |
-// padWithSpace adds whitespace to the input if the input is non-empty |
|
| 37 |
-func padWithSpace(source string, prefix, suffix int) string {
|
|
| 38 |
- if source == "" {
|
|
| 39 |
- return source |
|
| 40 |
- } |
|
| 41 |
- return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
|
|
| 42 |
-} |
| 43 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,38 +0,0 @@ |
| 1 |
-package templates |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bytes" |
|
| 5 |
- "testing" |
|
| 6 |
-) |
|
| 7 |
- |
|
| 8 |
-func TestParseStringFunctions(t *testing.T) {
|
|
| 9 |
- tm, err := Parse(`{{join (split . ":") "/"}}`)
|
|
| 10 |
- if err != nil {
|
|
| 11 |
- t.Fatal(err) |
|
| 12 |
- } |
|
| 13 |
- |
|
| 14 |
- var b bytes.Buffer |
|
| 15 |
- if err := tm.Execute(&b, "text:with:colon"); err != nil {
|
|
| 16 |
- t.Fatal(err) |
|
| 17 |
- } |
|
| 18 |
- want := "text/with/colon" |
|
| 19 |
- if b.String() != want {
|
|
| 20 |
- t.Fatalf("expected %s, got %s", want, b.String())
|
|
| 21 |
- } |
|
| 22 |
-} |
|
| 23 |
- |
|
| 24 |
-func TestNewParse(t *testing.T) {
|
|
| 25 |
- tm, err := NewParse("foo", "this is a {{ . }}")
|
|
| 26 |
- if err != nil {
|
|
| 27 |
- t.Fatal(err) |
|
| 28 |
- } |
|
| 29 |
- |
|
| 30 |
- var b bytes.Buffer |
|
| 31 |
- if err := tm.Execute(&b, "string"); err != nil {
|
|
| 32 |
- t.Fatal(err) |
|
| 33 |
- } |
|
| 34 |
- want := "this is a string" |
|
| 35 |
- if b.String() != want {
|
|
| 36 |
- t.Fatalf("expected %s, got %s", want, b.String())
|
|
| 37 |
- } |
|
| 38 |
-} |