2
Copyright The Helm Authors.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
25
"github.com/BurntSushi/toml"
26
"github.com/Masterminds/sprig/v3"
30
// funcMap returns a mapping of all of the functions that Engine has.
32
// Because some functions are late-bound (e.g. contain context-sensitive
33
// data), the functions may not all perform identically outside of an Engine
34
// as they will inside of an Engine.
36
// Known late-bound functions:
41
// These are late-bound in Engine.Render(). The
42
// version included in the FuncMap is a placeholder.
43
func funcMap() template.FuncMap {
44
f := sprig.TxtFuncMap()
46
delete(f, "expandenv")
48
// Add some extra functionality
49
extra := template.FuncMap{
53
"fromYamlArray": fromYAMLArray,
56
"fromJsonArray": fromJSONArray,
58
// This is a placeholder for the "include" function, which is
59
// late-bound to a template. By declaring it here, we preserve the
60
// integrity of the linter.
61
"include": func(string, interface{}) string { return "not implemented" },
62
"tpl": func(string, interface{}) interface{} { return "not implemented" },
63
"required": func(string, interface{}) (interface{}, error) { return "not implemented", nil },
64
// Provide a placeholder for the "lookup" function, which requires a kubernetes
66
"lookup": func(string, string, string, string) (map[string]interface{}, error) {
67
return map[string]interface{}{}, nil
71
for k, v := range extra {
78
// toYAML takes an interface, marshals it to yaml, and returns a string. It will
79
// always return a string, even on marshal error (empty string).
81
// This is designed to be called from a template.
82
func toYAML(v interface{}) string {
83
data, err := yaml.Marshal(v)
85
// Swallow errors inside of a template.
88
return strings.TrimSuffix(string(data), "\n")
91
// fromYAML converts a YAML document into a map[string]interface{}.
93
// This is not a general-purpose YAML parser, and will not parse all valid
94
// YAML documents. Additionally, because its intended use is within templates
95
// it tolerates errors. It will insert the returned error message string into
96
// m["Error"] in the returned map.
97
func fromYAML(str string) map[string]interface{} {
98
m := map[string]interface{}{}
100
if err := yaml.Unmarshal([]byte(str), &m); err != nil {
101
m["Error"] = err.Error()
106
// fromYAMLArray converts a YAML array into a []interface{}.
108
// This is not a general-purpose YAML parser, and will not parse all valid
109
// YAML documents. Additionally, because its intended use is within templates
110
// it tolerates errors. It will insert the returned error message string as
111
// the first and only item in the returned array.
112
func fromYAMLArray(str string) []interface{} {
115
if err := yaml.Unmarshal([]byte(str), &a); err != nil {
116
a = []interface{}{err.Error()}
121
// toTOML takes an interface, marshals it to toml, and returns a string. It will
122
// always return a string, even on marshal error (empty string).
124
// This is designed to be called from a template.
125
func toTOML(v interface{}) string {
126
b := bytes.NewBuffer(nil)
127
e := toml.NewEncoder(b)
135
// toJSON takes an interface, marshals it to json, and returns a string. It will
136
// always return a string, even on marshal error (empty string).
138
// This is designed to be called from a template.
139
func toJSON(v interface{}) string {
140
data, err := json.Marshal(v)
142
// Swallow errors inside of a template.
148
// fromJSON converts a JSON document into a map[string]interface{}.
150
// This is not a general-purpose JSON parser, and will not parse all valid
151
// JSON documents. Additionally, because its intended use is within templates
152
// it tolerates errors. It will insert the returned error message string into
153
// m["Error"] in the returned map.
154
func fromJSON(str string) map[string]interface{} {
155
m := make(map[string]interface{})
157
if err := json.Unmarshal([]byte(str), &m); err != nil {
158
m["Error"] = err.Error()
163
// fromJSONArray converts a JSON array into a []interface{}.
165
// This is not a general-purpose JSON parser, and will not parse all valid
166
// JSON documents. Additionally, because its intended use is within templates
167
// it tolerates errors. It will insert the returned error message string as
168
// the first and only item in the returned array.
169
func fromJSONArray(str string) []interface{} {
172
if err := json.Unmarshal([]byte(str), &a); err != nil {
173
a = []interface{}{err.Error()}