github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/internal/util/templates.go (about) 1 // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package util 6 7 import ( 8 "bytes" 9 "os" 10 "text/template" 11 ) 12 13 func ExecuteTemplateFile(file string, data any, funcs template.FuncMap) ([]byte, error) { 14 buf := bytes.NewBuffer([]byte{}) 15 t := template.New(file) 16 t.Funcs(funcs) 17 18 body, err := os.ReadFile(file) 19 if err != nil { 20 return nil, err 21 } 22 23 p, err := t.Parse(string(body)) 24 if err != nil { 25 return nil, err 26 } 27 28 err = p.Execute(buf, data) 29 if err != nil { 30 return nil, err 31 } 32 33 return buf.Bytes(), nil 34 }