mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 04:19:41 -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,
|
Template: t.config.ConsoleTitleTemplate,
|
||||||
Context: context,
|
Context: context,
|
||||||
}
|
}
|
||||||
return template.render()
|
text, err := template.render()
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *consoleTitle) getPwd() string {
|
func (t *consoleTitle) getPwd() string {
|
||||||
|
|
|
@ -275,5 +275,9 @@ func getConsoleBackgroundColor(env environmentInfo, backgroundColorTemplate stri
|
||||||
Template: backgroundColorTemplate,
|
Template: backgroundColorTemplate,
|
||||||
Context: context,
|
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 {
|
for _, template := range templates {
|
||||||
txtTemplate.Template = template
|
txtTemplate.Template = template
|
||||||
value := txtTemplate.render()
|
value, err := txtTemplate.render()
|
||||||
if value == "" {
|
if err != nil || value == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
|
|
|
@ -84,5 +84,9 @@ func (a *aws) string() string {
|
||||||
Template: segmentTemplate,
|
Template: segmentTemplate,
|
||||||
Context: a,
|
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,
|
Template: segmentTemplate,
|
||||||
Context: b,
|
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) {
|
func (b *batt) init(props *properties, env environmentInfo) {
|
||||||
|
|
|
@ -17,7 +17,11 @@ func (k *kubectl) string() string {
|
||||||
Template: segmentTemplate,
|
Template: segmentTemplate,
|
||||||
Context: k,
|
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) {
|
func (k *kubectl) init(props *properties, env environmentInfo) {
|
||||||
|
|
|
@ -47,7 +47,11 @@ func (s *session) enabled() bool {
|
||||||
Template: segmentTemplate,
|
Template: segmentTemplate,
|
||||||
Context: s,
|
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
|
return len(s.templateText) > 0
|
||||||
}
|
}
|
||||||
showDefaultUser := s.props.getBool(DisplayDefault, true)
|
showDefaultUser := s.props.getBool(DisplayDefault, true)
|
||||||
|
|
|
@ -25,7 +25,11 @@ func (t *tempus) enabled() bool {
|
||||||
Template: segmentTemplate,
|
Template: segmentTemplate,
|
||||||
Context: t,
|
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 len(t.templateText) > 0
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/Masterminds/sprig"
|
"github.com/Masterminds/sprig"
|
||||||
|
@ -18,16 +19,16 @@ type textTemplate struct {
|
||||||
Context interface{}
|
Context interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textTemplate) render() string {
|
func (t *textTemplate) render() (string, error) {
|
||||||
tmpl, err := template.New("title").Funcs(sprig.TxtFuncMap()).Parse(t.Template)
|
tmpl, err := template.New("title").Funcs(sprig.TxtFuncMap()).Parse(t.Template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return invalidTemplate
|
return "", errors.New(invalidTemplate)
|
||||||
}
|
}
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
defer buffer.Reset()
|
defer buffer.Reset()
|
||||||
err = tmpl.Execute(buffer, t.Context)
|
err = tmpl.Execute(buffer, t.Context)
|
||||||
if err != nil {
|
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) {
|
func TestRenderTemplate(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Case string
|
Case string
|
||||||
Expected string
|
Expected string
|
||||||
Template string
|
Template string
|
||||||
Context interface{}
|
ShouldError bool
|
||||||
|
Context interface{}
|
||||||
}{
|
}{
|
||||||
{Case: "single property", Expected: "hello world", Template: "{{.Text}} world", Context: struct{ Text string }{Text: "hello"}},
|
{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 property", ShouldError: true, 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 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 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: ""}},
|
{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,
|
Template: tc.Template,
|
||||||
Context: tc.Context,
|
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