diff --git a/templates/definition/defaults.yaml b/templates/definition/defaults.yaml index 57262bc5c..ab64da538 100644 --- a/templates/definition/defaults.yaml +++ b/templates/definition/defaults.yaml @@ -232,8 +232,8 @@ presets: {{- if ne .title "" }} title: {{ .title }} {{- end }} - user: '{{ .user }}' - password: '{{ .password }}' + user: {{ .user }} + password: {{ .password }} {{- if ne .capacity "" }} capacity: {{ .capacity }} {{- end }} diff --git a/templates/definition/vehicle/fiat.yaml b/templates/definition/vehicle/fiat.yaml index 55747dc51..2b18ba6b0 100644 --- a/templates/definition/vehicle/fiat.yaml +++ b/templates/definition/vehicle/fiat.yaml @@ -13,7 +13,7 @@ render: | type: fiat {{ include "vehicle-base" . }} {{- if ne .pin "" }} - pin: '{{ .pin }}' # mandatory to deep refresh SoC + pin: {{ .pin }} # mandatory to deep refresh SoC {{- end }} {{ include "vehicle-identify" . }} {{ include "vehicle-cloud" . }} diff --git a/util/templates/template.go b/util/templates/template.go index 17871df89..35e6f4098 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -326,14 +326,16 @@ func (t *Template) RenderResult(renderMode string, other map[string]interface{}) } tmpl := template.New("yaml") - var funcMap template.FuncMap = map[string]interface{}{} - // copied from: https://github.com/helm/helm/blob/8648ccf5d35d682dcd5f7a9c2082f0aaf071e817/pkg/engine/engine.go#L147-L154 - funcMap["include"] = func(name string, data interface{}) (string, error) { - buf := bytes.NewBuffer(nil) - if err := tmpl.ExecuteTemplate(buf, name, data); err != nil { - return "", err - } - return buf.String(), nil + funcMap := template.FuncMap{ + // include function + // copied from: https://github.com/helm/helm/blob/8648ccf5d35d682dcd5f7a9c2082f0aaf071e817/pkg/engine/engine.go#L147-L154 + "include": func(name string, data interface{}) (string, error) { + buf := bytes.NewBuffer(nil) + if err := tmpl.ExecuteTemplate(buf, name, data); err != nil { + return "", err + } + return buf.String(), nil + }, } tmpl, err := tmpl.Funcs(template.FuncMap(sprig.FuncMap())).Funcs(funcMap).Parse(t.Render) diff --git a/util/templates/utils.go b/util/templates/utils.go index 56598a40a..eb8d35854 100644 --- a/util/templates/utils.go +++ b/util/templates/utils.go @@ -7,7 +7,17 @@ import ( "gopkg.in/yaml.v3" ) +func quote(value string) string { + quoted := strings.ReplaceAll(value, `'`, `''`) + return fmt.Sprintf("'%s'", quoted) +} + func yamlQuote(value string) string { + // don't quote empty strings + if value == "" { + return value + } + input := fmt.Sprintf("key: %s", value) var res struct { @@ -15,8 +25,12 @@ func yamlQuote(value string) string { } if err := yaml.Unmarshal([]byte(input), &res); err != nil || value != res.Value { - quoted := strings.ReplaceAll(value, `'`, `''`) - return fmt.Sprintf("'%s'", quoted) + return quote(value) + } + + // fix 0815, but not 0 + if strings.HasPrefix("0", value) && len(value) > 1 { + return quote(value) } return value diff --git a/util/templates/utils_test.go b/util/templates/utils_test.go index da82f42d9..1883b750f 100644 --- a/util/templates/utils_test.go +++ b/util/templates/utils_test.go @@ -8,7 +8,7 @@ import ( ) func TestYamlDecode(t *testing.T) { - for _, value := range []string{`value`, `!value`, `@value`, `"value"`, `"va"lue"`, `va'lue`, `@va'lue`} { + for _, value := range []string{`value`, `!value`, `@value`, `"value"`, `"va"lue"`, `va'lue`, `@va'lue`, `0815`, `4711`, ``} { t.Run(value, func(t *testing.T) { quoted := yamlQuote(value) input := fmt.Sprintf("key: %s", quoted)