initFunMap creates the Engine's FuncMap and adds context-specific functions.
(t *template.Template)
| 196 | |
| 197 | // initFunMap creates the Engine's FuncMap and adds context-specific functions. |
| 198 | func (e Engine) initFunMap(t *template.Template) { |
| 199 | funcMap := funcMap() |
| 200 | includedNames := make(map[string]int) |
| 201 | |
| 202 | // Add the template-rendering functions here so we can close over t. |
| 203 | funcMap["include"] = includeFun(t, includedNames) |
| 204 | funcMap["tpl"] = tplFun(t, includedNames, e.Strict) |
| 205 | |
| 206 | // Add the `required` function here so we can use lintMode |
| 207 | funcMap["required"] = func(warn string, val any) (any, error) { |
| 208 | if val == nil { |
| 209 | if e.LintMode { |
| 210 | // Don't fail on missing required values when linting |
| 211 | slog.Warn("missing required value", "message", warn) |
| 212 | return "", nil |
| 213 | } |
| 214 | return val, errors.New(warnWrap(warn)) |
| 215 | } else if _, ok := val.(string); ok { |
| 216 | if val == "" { |
| 217 | if e.LintMode { |
| 218 | // Don't fail on missing required values when linting |
| 219 | slog.Warn("missing required values", "message", warn) |
| 220 | return "", nil |
| 221 | } |
| 222 | return val, errors.New(warnWrap(warn)) |
| 223 | } |
| 224 | } |
| 225 | return val, nil |
| 226 | } |
| 227 | |
| 228 | // Override sprig fail function for linting and wrapping message |
| 229 | funcMap["fail"] = func(msg string) (string, error) { |
| 230 | if e.LintMode { |
| 231 | // Don't fail when linting |
| 232 | slog.Info("funcMap fail", "message", msg) |
| 233 | return "", nil |
| 234 | } |
| 235 | return "", errors.New(warnWrap(msg)) |
| 236 | } |
| 237 | |
| 238 | // If we are not linting and have a cluster connection, provide a Kubernetes-backed |
| 239 | // implementation. |
| 240 | if !e.LintMode && e.clientProvider != nil { |
| 241 | funcMap["lookup"] = newLookupFunction(*e.clientProvider) |
| 242 | } |
| 243 | |
| 244 | // When DNS lookups are not enabled override the sprig function and return |
| 245 | // an empty string. |
| 246 | if !e.EnableDNS { |
| 247 | funcMap["getHostByName"] = func(_ string) string { |
| 248 | return "" |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Set custom template funcs |
| 253 | maps.Copy(funcMap, e.CustomTemplateFuncs) |
| 254 | |
| 255 | t.Funcs(funcMap) |
no test coverage detected