MCPcopy Create free account
hub / github.com/cozystack/talm / initFunMap

Method initFunMap

pkg/engine/helm/engine.go:204–269  ·  view source on GitHub ↗

initFunMap creates the Engine's FuncMap and adds context-specific functions.

(tmpl *template.Template)

Source from the content-addressed store, hash-verified

202
203// initFunMap creates the Engine's FuncMap and adds context-specific functions.
204func (e Engine) initFunMap(tmpl *template.Template) {
205 funcMap := funcMap()
206 includedNames := make(map[string]int)
207
208 // Add the template-rendering functions here so we can close over tmpl.
209 funcMap[helmFuncInclude] = includeFun(tmpl, includedNames)
210 funcMap[helmFuncTpl] = tplFun(tmpl, includedNames, e.Strict)
211
212 // Add the `required` function here so we can use lintMode
213 funcMap[helmFuncRequired] = func(warn string, val any) (any, error) {
214 // A required value is considered missing when it is nil, or
215 // when it is the empty string. Anything else passes through.
216 missing := val == nil
217 if !missing {
218 if str, ok := val.(string); ok && str == "" {
219 missing = true
220 }
221 }
222
223 if !missing {
224 return val, nil
225 }
226
227 if e.LintMode {
228 // Don't fail on missing required values when linting
229 log.Printf("[INFO] Missing required value: %s", warn)
230
231 return "", nil
232 }
233
234 return val, errors.New(warnWrap(warn))
235 }
236
237 // Override sprig fail function for linting and wrapping message
238 funcMap["fail"] = func(msg string) (string, error) {
239 if e.LintMode {
240 // Don't fail when linting
241 log.Printf("[INFO] Fail: %s", msg)
242
243 return "", nil
244 }
245
246 return "", errors.New(warnWrap(msg))
247 }
248
249 // If we are not linting and have a cluster connection, provide a Kubernetes-backed
250 // implementation.
251 if !e.LintMode {
252 funcMap[helmFuncLookup] = LookupFunc
253 }
254
255 // When DNS lookups are not enabled override the sprig function and return
256 // an empty string.
257 if !e.EnableDNS {
258 funcMap["getHostByName"] = func(_ string) string {
259 return ""
260 }
261 }

Callers 1

renderMethod · 0.95

Calls 4

funcMapFunction · 0.85
includeFunFunction · 0.85
tplFunFunction · 0.85
warnWrapFunction · 0.85

Tested by

no test coverage detected