mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Merge pull request #587 from replicatedhq/template-delims
Allow for repl{{ template delim
This commit is contained in:
@@ -8,7 +8,7 @@ spec:
|
||||
selector:
|
||||
- app=example
|
||||
- component=nginx
|
||||
namespace: repl{{ Namespace }}
|
||||
namespace: default
|
||||
limits:
|
||||
maxAge: 720h # 30*24
|
||||
maxLines: 10000
|
||||
|
||||
@@ -25,20 +25,35 @@ func Bool(text string, data interface{}) (bool, error) {
|
||||
}
|
||||
|
||||
func Execute(text string, data interface{}) (string, error) {
|
||||
tmpl, err := template.New(text).
|
||||
Delims("{{repl", "}}").
|
||||
Funcs(funcMap).
|
||||
Parse(text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
delims := []struct {
|
||||
ldelim string
|
||||
rdelim string
|
||||
}{
|
||||
{"{{repl", "}}"},
|
||||
{"repl{{", "}}"},
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
err = func() (err error) {
|
||||
defer errRecover(&err)
|
||||
err = tmpl.Execute(&buf, data)
|
||||
return
|
||||
}()
|
||||
return buf.String(), err
|
||||
|
||||
curText := text
|
||||
for _, d := range delims {
|
||||
tmpl, err := template.New(curText).
|
||||
Delims(d.ldelim, d.rdelim).
|
||||
Funcs(funcMap).
|
||||
Parse(curText)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
err = func() (err error) {
|
||||
defer errRecover(&err)
|
||||
err = tmpl.Execute(&buf, data)
|
||||
return
|
||||
}()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
curText = buf.String()
|
||||
}
|
||||
return curText, nil
|
||||
}
|
||||
|
||||
func RegisterFunc(key string, fn interface{}) {
|
||||
|
||||
45
pkg/convert/templates_test.go
Normal file
45
pkg/convert/templates_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package convert
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExecute(t *testing.T) {
|
||||
type args struct {
|
||||
text string
|
||||
data interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "{{repl",
|
||||
args: args{
|
||||
text: "{{repl printf \"%s\" \"hello\"}}",
|
||||
data: nil,
|
||||
},
|
||||
want: "hello",
|
||||
},
|
||||
{
|
||||
name: "repl{{",
|
||||
args: args{
|
||||
text: "repl{{ printf \"%s\" \"hello\"}}",
|
||||
data: nil,
|
||||
},
|
||||
want: "hello",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := Execute(tt.args.text, tt.args.data)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("Execute() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user