@@ -17,6 +17,7 @@ limitations under the License.
1717package util
1818
1919import (
20+ "bufio"
2021 "bytes"
2122 "fmt"
2223 "os"
@@ -138,6 +139,82 @@ func ExecuteTemplate(templateFile string, data interface{}) (string, error) {
138139 return renderedTemplate , nil
139140}
140141
142+ // template functions
143+ var tmpl * template.Template
144+
145+ // template function which allows to execute a template from within
146+ // a template file.
147+ // name - name of the template as defined with with `{{define "some-template"}}your template{{end}}
148+ // data - data to pass into to render the template for all can use `.`
149+ func execTempl (name string , data interface {}) (string , error ) {
150+ buf := & bytes.Buffer {}
151+ err := tmpl .ExecuteTemplate (buf , name , data )
152+ return buf .String (), err
153+ }
154+
155+ // template function to indent the template with n tabs
156+ func indent (n int , in string ) string {
157+ var out string
158+ s := bufio .NewScanner (bytes .NewReader ([]byte (in )))
159+ for s .Scan () {
160+ line := strings .TrimSpace (s .Text ())
161+ for i := 0 ; i < n ; i ++ {
162+ line = "\t " + line
163+ }
164+ out += line + "\n "
165+ }
166+ return out
167+ }
168+
169+ // template function to remove empty lines if there are > n continuous empty lines
170+ func removeNewLines (n int , in string ) string {
171+ var out string
172+ s := bufio .NewScanner (bytes .NewReader ([]byte (in )))
173+
174+ // Variable to keep track of consecutive empty lines
175+ emptyLineCount := 0
176+ for s .Scan () {
177+ line := s .Text ()
178+
179+ if strings .TrimSpace (line ) == "" {
180+ emptyLineCount ++
181+ // If we have already seen more then n empty lines, skip this one
182+ if emptyLineCount > n {
183+ continue
184+ }
185+ } else {
186+ // Reset the empty line counter when we encounter a non-empty line
187+ emptyLineCount = 0
188+ }
189+
190+ out += line + "\n "
191+ }
192+ return out
193+ }
194+
195+ // This function removes extra space and new-lines from conf data.
196+ func removeNewLinesInSections (in string ) string {
197+ var out string
198+ s := bufio .NewScanner (bytes .NewReader ([]byte (in )))
199+
200+ for s .Scan () {
201+ line := strings .TrimSpace (s .Text ())
202+
203+ if line != "" {
204+ if strings .HasPrefix (line , "[" ) && strings .HasSuffix (line , "]" ) {
205+ // new section-header
206+ if len (out ) > 0 {
207+ out += "\n "
208+ }
209+ }
210+
211+ out += line + "\n "
212+ }
213+ }
214+
215+ return out
216+ }
217+
141218// template function to increment an int
142219func add (x , y int ) int {
143220 return x + y
@@ -153,11 +230,16 @@ func lower(s string) string {
153230func ExecuteTemplateData (templateData string , data interface {}) (string , error ) {
154231
155232 var buff bytes.Buffer
233+ var err error
156234 funcs := template.FuncMap {
157- "add" : add ,
158- "lower" : lower ,
235+ "add" : add ,
236+ "execTempl" : execTempl ,
237+ "indent" : indent ,
238+ "lower" : lower ,
239+ "removeNewLines" : removeNewLines ,
240+ "removeNewLinesInSections" : removeNewLinesInSections ,
159241 }
160- tmpl , err : = template .New ("tmp" ).Option ("missingkey=error" ).Funcs (funcs ).Parse (templateData )
242+ tmpl , err = template .New ("tmp" ).Option ("missingkey=error" ).Funcs (funcs ).Parse (templateData )
161243 if err != nil {
162244 return "" , err
163245 }
@@ -193,10 +275,14 @@ func ExecuteTemplateFile(filename string, data interface{}) (string, error) {
193275 file := string (b )
194276 var buff bytes.Buffer
195277 funcs := template.FuncMap {
196- "add" : add ,
197- "lower" : lower ,
278+ "add" : add ,
279+ "execTempl" : execTempl ,
280+ "indent" : indent ,
281+ "lower" : lower ,
282+ "removeNewLines" : removeNewLines ,
283+ "removeNewLinesInSections" : removeNewLinesInSections ,
198284 }
199- tmpl , err : = template .New ("tmp" ).Option ("missingkey=error" ).Funcs (funcs ).Parse (file )
285+ tmpl , err = template .New ("tmp" ).Option ("missingkey=error" ).Funcs (funcs ).Parse (file )
200286 if err != nil {
201287 return "" , err
202288 }
0 commit comments