fix: return error on template render

This commit is contained in:
Jan De Dobbeleer 2021-04-11 15:24:03 +02:00 committed by Jan De Dobbeleer
parent ee93191aac
commit 2990bc2536
10 changed files with 55 additions and 20 deletions

View file

@ -65,7 +65,11 @@ func (t *consoleTitle) getTemplateText() string {
Template: t.config.ConsoleTitleTemplate,
Context: context,
}
return template.render()
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (t *consoleTitle) getPwd() string {

View file

@ -275,5 +275,9 @@ func getConsoleBackgroundColor(env environmentInfo, backgroundColorTemplate stri
Template: backgroundColorTemplate,
Context: context,
}
return template.render()
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}

View file

@ -170,8 +170,8 @@ func (segment *Segment) getColor(templates []string, defaultColor string) string
}
for _, template := range templates {
txtTemplate.Template = template
value := txtTemplate.render()
if value == "" {
value, err := txtTemplate.render()
if err != nil || value == "" {
continue
}
return value

View file

@ -84,5 +84,9 @@ func (a *aws) string() string {
Template: segmentTemplate,
Context: a,
}
return template.render()
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}

View file

@ -89,7 +89,11 @@ func (b *batt) string() string {
Template: segmentTemplate,
Context: b,
}
return template.render()
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (b *batt) init(props *properties, env environmentInfo) {

View file

@ -17,7 +17,11 @@ func (k *kubectl) string() string {
Template: segmentTemplate,
Context: k,
}
return template.render()
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (k *kubectl) init(props *properties, env environmentInfo) {

View file

@ -47,7 +47,11 @@ func (s *session) enabled() bool {
Template: segmentTemplate,
Context: s,
}
s.templateText = template.render()
var err error
s.templateText, err = template.render()
if err != nil {
s.templateText = err.Error()
}
return len(s.templateText) > 0
}
showDefaultUser := s.props.getBool(DisplayDefault, true)

View file

@ -25,7 +25,11 @@ func (t *tempus) enabled() bool {
Template: segmentTemplate,
Context: t,
}
t.templateText = template.render()
var err error
t.templateText, err = template.render()
if err != nil {
t.templateText = err.Error()
}
return len(t.templateText) > 0
}
return true

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"errors"
"text/template"
"github.com/Masterminds/sprig"
@ -18,16 +19,16 @@ type textTemplate struct {
Context interface{}
}
func (t *textTemplate) render() string {
func (t *textTemplate) render() (string, error) {
tmpl, err := template.New("title").Funcs(sprig.TxtFuncMap()).Parse(t.Template)
if err != nil {
return invalidTemplate
return "", errors.New(invalidTemplate)
}
buffer := new(bytes.Buffer)
defer buffer.Reset()
err = tmpl.Execute(buffer, t.Context)
if err != nil {
return incorrectTemplate
return "", errors.New(incorrectTemplate)
}
return buffer.String()
return buffer.String(), nil
}

View file

@ -8,14 +8,15 @@ import (
func TestRenderTemplate(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
Context interface{}
Case string
Expected string
Template string
ShouldError bool
Context interface{}
}{
{Case: "single property", Expected: "hello world", Template: "{{.Text}} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid property", Expected: incorrectTemplate, Template: "{{.Durp}} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid template", Expected: invalidTemplate, Template: "{{ if .Text }} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid property", ShouldError: true, Template: "{{.Durp}} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid template", ShouldError: true, Template: "{{ if .Text }} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "if statement true", Expected: "hello world", Template: "{{ if .Text }}{{.Text}} world{{end}}", Context: struct{ Text string }{Text: "hello"}},
{Case: "if statement false", Expected: "world", Template: "{{ if .Text }}{{.Text}} {{end}}world", Context: struct{ Text string }{Text: ""}},
{
@ -72,6 +73,11 @@ func TestRenderTemplate(t *testing.T) {
Template: tc.Template,
Context: tc.Context,
}
assert.Equal(t, tc.Expected, template.render(), tc.Case)
text, err := template.render()
if tc.ShouldError {
assert.Error(t, err)
continue
}
assert.Equal(t, tc.Expected, text, tc.Case)
}
}