mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 11:59:40 -08:00
fix: return error on template render
This commit is contained in:
parent
ee93191aac
commit
2990bc2536
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue