ApplyBinding renders an input template using bindings in Go templating format
(input string, bindings map[string]string)
| 25 | |
| 26 | // ApplyBinding renders an input template using bindings in Go templating format |
| 27 | func ApplyBinding(input string, bindings map[string]string) (string, error) { |
| 28 | if input == "" { |
| 29 | return "", nil |
| 30 | } |
| 31 | |
| 32 | if bindings == nil { |
| 33 | bindings = make(map[string]string) |
| 34 | } |
| 35 | |
| 36 | // Support both `.inputs.foo` and `inputs.foo` |
| 37 | input = inputsPrefixRegexp.ReplaceAllString(input, "{{ .inputs.") |
| 38 | |
| 39 | tmpl, err := template.New("chainloop").Option("missingkey=zero").Parse(input) |
| 40 | if err != nil { |
| 41 | return "", err |
| 42 | } |
| 43 | |
| 44 | // Only support placeholders that are prefixed with "inputs.", ex `{{ inputs.foo }} |
| 45 | namespacedBinding := map[string]any{"inputs": bindings} |
| 46 | |
| 47 | buffer := new(bytes.Buffer) |
| 48 | err = tmpl.Execute(buffer, namespacedBinding) |
| 49 | if err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | |
| 53 | return buffer.String(), nil |
| 54 | } |
no test coverage detected