processConfigFile processes a config file, rendering it as a template if enabled
(data []byte, filename string)
| 225 | |
| 226 | // processConfigFile processes a config file, rendering it as a template if enabled |
| 227 | func processConfigFile(data []byte, filename string) ([]byte, error) { |
| 228 | // If the config file contains "enable-template" in a comment within the first 1KB, then |
| 229 | // we will treat the file as a template and render it. |
| 230 | templating, err := checkTemplatingEnabled(data) |
| 231 | if err != nil { |
| 232 | return nil, err |
| 233 | } |
| 234 | |
| 235 | if !templating { |
| 236 | return data, nil |
| 237 | } |
| 238 | |
| 239 | stdlog.Printf("Processing config file as template; filename=%v\n", filename) |
| 240 | tpl, err := template.New(filename).Funcs(sprig.FuncMap()).Parse(string(data)) |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | |
| 245 | var rendered bytes.Buffer |
| 246 | err = tpl.Execute(&rendered, nil) |
| 247 | if err != nil { |
| 248 | return nil, err |
| 249 | } |
| 250 | |
| 251 | return rendered.Bytes(), nil |
| 252 | } |
| 253 | |
| 254 | func loadAndUnmarshalContent(content []byte, filename string, config any) error { |
| 255 | processed, err := processConfigFile(content, filename) |