github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/generators/render.go (about) 1 package generators 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "text/template" 8 ) 9 10 func RenderTemplate(path string, tpl string, data any) error { 11 // Assume that templates without data are just files 12 if data == nil { 13 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 14 return err 15 } 16 17 return ioutil.WriteFile(path, []byte(tpl), 0664) 18 } 19 20 t, err := template.New(path).Parse(tpl) 21 if err != nil { 22 return err 23 } 24 25 if err := os.MkdirAll(filepath.Dir(t.Name()), 0755); err != nil { 26 return err 27 } 28 29 dst, err := os.OpenFile(t.Name(), os.O_WRONLY|os.O_CREATE, 0664) 30 if err != nil { 31 return err 32 } 33 defer dst.Close() 34 35 return t.Execute(dst, data) 36 }