1 /* 2 Copyright The Helm Authors. 3 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 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 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. 15 */ 16 17 package engine 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "strings" 23 "text/template" 24 25 "github.com/BurntSushi/toml" 26 "github.com/Masterminds/sprig/v3" 27 "sigs.k8s.io/yaml" 28 ) 29 30 // funcMap returns a mapping of all of the functions that Engine has. 31 // 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. 35 // 36 // Known late-bound functions: 37 // 38 // - "include" 39 // - "tpl" 40 // 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() 45 delete(f, "env") 46 delete(f, "expandenv") 47 48 // Add some extra functionality 49 extra := template.FuncMap{ 50 "toToml": toTOML, 51 "toYaml": toYAML, 52 "fromYaml": fromYAML, 53 "fromYamlArray": fromYAMLArray, 54 "toJson": toJSON, 55 "fromJson": fromJSON, 56 "fromJsonArray": fromJSONArray, 57 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 65 // connection. 66 "lookup": func(string, string, string, string) (map[string]interface{}, error) { 67 return map[string]interface{}{}, nil 68 }, 69 } 70 71 for k, v := range extra { 72 f[k] = v 73 } 74 75 return f 76 } 77 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). 80 // 81 // This is designed to be called from a template. 82 func toYAML(v interface{}) string { 83 data, err := yaml.Marshal(v) 84 if err != nil { 85 // Swallow errors inside of a template. 86 return "" 87 } 88 return strings.TrimSuffix(string(data), "\n") 89 } 90 91 // fromYAML converts a YAML document into a map[string]interface{}. 92 // 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{}{} 99 100 if err := yaml.Unmarshal([]byte(str), &m); err != nil { 101 m["Error"] = err.Error() 102 } 103 return m 104 } 105 106 // fromYAMLArray converts a YAML array into a []interface{}. 107 // 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{} { 113 a := []interface{}{} 114 115 if err := yaml.Unmarshal([]byte(str), &a); err != nil { 116 a = []interface{}{err.Error()} 117 } 118 return a 119 } 120 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). 123 // 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) 128 err := e.Encode(v) 129 if err != nil { 130 return err.Error() 131 } 132 return b.String() 133 } 134 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). 137 // 138 // This is designed to be called from a template. 139 func toJSON(v interface{}) string { 140 data, err := json.Marshal(v) 141 if err != nil { 142 // Swallow errors inside of a template. 143 return "" 144 } 145 return string(data) 146 } 147 148 // fromJSON converts a JSON document into a map[string]interface{}. 149 // 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{}) 156 157 if err := json.Unmarshal([]byte(str), &m); err != nil { 158 m["Error"] = err.Error() 159 } 160 return m 161 } 162 163 // fromJSONArray converts a JSON array into a []interface{}. 164 // 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{} { 170 a := []interface{}{} 171 172 if err := json.Unmarshal([]byte(str), &a); err != nil { 173 a = []interface{}{err.Error()} 174 } 175 return a 176 } 177