1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00

Support template as raw string.

This commit is contained in:
Ludovic Fernandez 2017-11-17 10:12:03 +01:00 committed by Traefiker
parent 8719f2836e
commit 722f299306
2 changed files with 67 additions and 15 deletions

View File

@ -52,10 +52,6 @@ func (p *BaseProvider) MatchConstraints(tags []string) (bool, *types.Constraint)
// GetConfiguration return the provider configuration using templating
func (p *BaseProvider) GetConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
var (
buf []byte
err error
)
configuration := new(types.Configuration)
var defaultFuncMap = sprig.TxtFuncMap()
@ -68,18 +64,13 @@ func (p *BaseProvider) GetConfiguration(defaultTemplateFile string, funcMap temp
}
tmpl := template.New(p.Filename).Funcs(defaultFuncMap)
if len(p.Filename) > 0 {
buf, err = ioutil.ReadFile(p.Filename)
if err != nil {
return nil, err
}
} else {
buf, err = autogen.Asset(defaultTemplateFile)
if err != nil {
return nil, err
}
tmplContent, err := p.getTemplateContent(defaultTemplateFile)
if err != nil {
return nil, err
}
_, err = tmpl.Parse(string(buf))
_, err = tmpl.Parse(tmplContent)
if err != nil {
return nil, err
}
@ -100,6 +91,26 @@ func (p *BaseProvider) GetConfiguration(defaultTemplateFile string, funcMap temp
return configuration, nil
}
func (p *BaseProvider) getTemplateContent(defaultTemplateFile string) (string, error) {
if len(p.Filename) > 0 {
buf, err := ioutil.ReadFile(p.Filename)
if err != nil {
return "", err
}
return string(buf), nil
}
if strings.HasSuffix(defaultTemplateFile, ".tmpl") {
buf, err := autogen.Asset(defaultTemplateFile)
if err != nil {
return "", err
}
return string(buf), nil
}
return defaultTemplateFile, nil
}
func split(sep, s string) []string {
return strings.Split(s, sep)
}

View File

@ -8,6 +8,8 @@ import (
"text/template"
"github.com/containous/traefik/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type myProvider struct {
@ -431,3 +433,42 @@ func TestSprigFunctions(t *testing.T) {
t.Fatal("Frontend frontend-1 should exists, but it not")
}
}
func TestBaseProvider_GetConfiguration(t *testing.T) {
baseProvider := BaseProvider{}
testCases := []struct {
name string
defaultTemplateFile string
expectedContent string
}{
{
defaultTemplateFile: "templates/docker.tmpl",
expectedContent: readTemplateFile(t, "./../templates/docker.tmpl"),
},
{
defaultTemplateFile: `template content`,
expectedContent: `template content`,
},
}
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
content, err := baseProvider.getTemplateContent(test.defaultTemplateFile)
require.NoError(t, err)
assert.Equal(t, test.expectedContent, content)
})
}
}
func readTemplateFile(t *testing.T, path string) string {
t.Helper()
expectedContent, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return string(expectedContent)
}