/
template.go
33 lines (30 loc) · 909 Bytes
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// rabtap templating
// Copyright (C) 2017-2021 Jan Delgado
package main
import (
"bytes"
"text/template"
)
// MergeTemplateFuncs merges the given list of template funcs into a single one
func MergeTemplateFuncs(maps ...template.FuncMap) template.FuncMap {
res := template.FuncMap{}
for _, m := range maps {
for k, v := range m {
res[k] = v
}
}
return res
}
// resolveTemplate resolves a template for use in the broker info printer,
// with support for colored output. name is just an informational name
// passed to the template ctor. tpl is the actual template and args
// the arguments used during rendering.
func resolveTemplate(name string, tpl string, args interface{}, funcs map[string]interface{}) string {
tmpl := template.Must(template.New(name).Funcs(funcs).Parse(tpl))
var buf bytes.Buffer
err := tmpl.Execute(&buf, args)
if err != nil {
panic(err)
}
return buf.String()
}