@@ -17,6 +17,7 @@ import (
1717 "bytes"
1818 "errors"
1919 "fmt"
20+ "io/fs"
2021 "path/filepath"
2122 "reflect"
2223 "strings"
@@ -55,9 +56,15 @@ func plus(a, b int) int {
5556 return a + b
5657}
5758
58- var TemplateFunctions = templateFunctions ()
59+ func TemplateFunctions (templateFs fs.FS ) template.FuncMap {
60+ return functionsData {templateFS : templateFs }.templateFunctions ()
61+ }
62+
63+ type functionsData struct {
64+ templateFS fs.FS
65+ }
5966
60- func templateFunctions () template.FuncMap {
67+ func ( t functionsData ) templateFunctions () template.FuncMap {
6168 return template.FuncMap {
6269 "title" : SpaceSeparatedTitle ,
6370 "replace" : strings .Replace ,
@@ -76,8 +83,8 @@ func templateFunctions() template.FuncMap {
7683 "sub" : subtract ,
7784 "plus" : plus ,
7885 "firstSentence" : FirstSentence ,
79- "trimTemplate" : TrimTemplate ,
80- "customTemplate" : executeCustomTemplate ,
86+ "trimTemplate" : t . trimTemplate ,
87+ "customTemplate" : t . customTemplate ,
8188 }
8289}
8390
@@ -95,38 +102,36 @@ func structToPtr(e any) reflect.Value {
95102
96103// Temporary function to simulate how Ruby MMv1's lines() function works
97104// for nested documentation. Can replace with normal "template" after switchover
98- func TrimTemplate ( templatePath string , e any ) string {
105+ func ( t * functionsData ) trimTemplate ( templatePath string , e any ) ( string , error ) {
99106 templates := []string {
100107 fmt .Sprintf ("templates/terraform/%s" , templatePath ),
101108 "templates/terraform/expand_resource_ref.tmpl" ,
102109 }
103110 templateFileName := filepath .Base (templatePath )
104111
105- // Need to remake TemplateFunctions, referencing it directly here
106- // causes a declaration loop
107- tmpl , err := template .New (templateFileName ).Funcs (templateFunctions ()).ParseFiles (templates ... )
112+ tmpl , err := template .New (templateFileName ).Funcs (t .templateFunctions ()).ParseFS (t .templateFS , templates ... )
108113 if err != nil {
109- glog . Exit ( err )
114+ return "" , err
110115 }
111116
112117 contents := bytes.Buffer {}
113118 if err = tmpl .ExecuteTemplate (& contents , templateFileName , structToPtr (e )); err != nil {
114- glog . Exit ( err )
119+ return "" , err
115120 }
116121
117122 rs := contents .String ()
118123
119124 if rs == "" {
120- return rs
125+ return "" , nil
121126 }
122127
123128 for strings .HasSuffix (rs , "\n " ) {
124129 rs = strings .TrimSuffix (rs , "\n " )
125130 }
126- return fmt .Sprintf ("%s\n " , rs )
131+ return fmt .Sprintf ("%s\n " , rs ), nil
127132}
128133
129- func executeCustomTemplate ( e any , templatePath string , appendNewline bool ) string {
134+ func ( t functionsData ) customTemplate ( e any , templatePath string , appendNewline bool ) ( string , error ) {
130135 templates := []string {
131136 templatePath ,
132137 "templates/terraform/expand_resource_ref.tmpl" ,
@@ -139,9 +144,9 @@ func executeCustomTemplate(e any, templatePath string, appendNewline bool) strin
139144 }
140145 templateFileName := filepath .Base (templatePath )
141146
142- tmpl , err := template .New (templateFileName ).Funcs (templateFunctions ()).ParseFiles ( templates ... )
147+ tmpl , err := template .New (templateFileName ).Funcs (t . templateFunctions ()).ParseFS ( t . templateFS , templates ... )
143148 if err != nil {
144- glog . Exit ( err )
149+ return "" , err
145150 }
146151
147152 contents := bytes.Buffer {}
@@ -157,5 +162,5 @@ func executeCustomTemplate(e any, templatePath string, appendNewline bool) strin
157162 if ! appendNewline {
158163 rs = strings .TrimSuffix (rs , "\n " )
159164 }
160- return rs
165+ return rs , nil
161166}
0 commit comments