Provide basic string manupilation functions for template executions.

This change centralizes the template manipulation in a single package
and adds basic string functions to their execution.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera
2016-03-04 12:29:44 -05:00
parent 44b56341d9
commit 8514880997
9 changed files with 155 additions and 23 deletions

View File

@@ -9,6 +9,7 @@ import (
"text/template"
"github.com/docker/docker/reference"
"github.com/docker/docker/utils/templates"
"github.com/docker/engine-api/types"
)
@@ -54,7 +55,7 @@ func (c *Context) preformat() {
}
func (c *Context) parseFormat() (*template.Template, error) {
tmpl, err := template.New("").Parse(c.finalFormat)
tmpl, err := templates.Parse(c.finalFormat)
if err != nil {
c.buffer.WriteString(fmt.Sprintf("Template parsing error: %v\n", err))
c.buffer.WriteTo(c.Output)

View File

@@ -1,23 +1,15 @@
package client
import (
"encoding/json"
"fmt"
"text/template"
"github.com/docker/docker/api/client/inspect"
Cli "github.com/docker/docker/cli"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/utils/templates"
"github.com/docker/engine-api/client"
)
var funcMap = template.FuncMap{
"json": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}
// CmdInspect displays low-level information on one or more containers or images.
//
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
@@ -123,7 +115,7 @@ func (cli *DockerCli) inspectErrorStatus(err error) (status int) {
func (cli *DockerCli) newInspectorWithTemplate(tmplStr string) (inspect.Inspector, error) {
elementInspector := inspect.NewIndentedInspector(cli.out)
if tmplStr != "" {
tmpl, err := template.New("").Funcs(funcMap).Parse(tmplStr)
tmpl, err := templates.Parse(tmplStr)
if err != nil {
return nil, fmt.Errorf("Template parsing error: %s", err)
}

View File

@@ -4,7 +4,8 @@ import (
"bytes"
"strings"
"testing"
"text/template"
"github.com/docker/docker/utils/templates"
)
type testElement struct {
@@ -13,7 +14,7 @@ type testElement struct {
func TestTemplateInspectorDefault(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.DNS}}")
tmpl, err := templates.Parse("{{.DNS}}")
if err != nil {
t.Fatal(err)
}
@@ -32,7 +33,7 @@ func TestTemplateInspectorDefault(t *testing.T) {
func TestTemplateInspectorEmpty(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.DNS}}")
tmpl, err := templates.Parse("{{.DNS}}")
if err != nil {
t.Fatal(err)
}
@@ -48,7 +49,7 @@ func TestTemplateInspectorEmpty(t *testing.T) {
func TestTemplateInspectorTemplateError(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.Foo}}")
tmpl, err := templates.Parse("{{.Foo}}")
if err != nil {
t.Fatal(err)
}
@@ -66,7 +67,7 @@ func TestTemplateInspectorTemplateError(t *testing.T) {
func TestTemplateInspectorRawFallback(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.Dns}}")
tmpl, err := templates.Parse("{{.Dns}}")
if err != nil {
t.Fatal(err)
}
@@ -85,7 +86,7 @@ func TestTemplateInspectorRawFallback(t *testing.T) {
func TestTemplateInspectorRawFallbackError(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.Dns}}")
tmpl, err := templates.Parse("{{.Dns}}")
if err != nil {
t.Fatal(err)
}
@@ -102,7 +103,7 @@ func TestTemplateInspectorRawFallbackError(t *testing.T) {
func TestTemplateInspectorMultiple(t *testing.T) {
b := new(bytes.Buffer)
tmpl, err := template.New("test").Parse("{{.DNS}}")
tmpl, err := templates.Parse("{{.DNS}}")
if err != nil {
t.Fatal(err)
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/docker/docker/dockerversion"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/utils"
"github.com/docker/docker/utils/templates"
"github.com/docker/engine-api/types"
)
@@ -48,7 +49,7 @@ func (cli *DockerCli) CmdVersion(args ...string) (err error) {
}
var tmpl *template.Template
if tmpl, err = template.New("").Funcs(funcMap).Parse(templateFormat); err != nil {
if tmpl, err = templates.Parse(templateFormat); err != nil {
return Cli.StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()}
}