-
Notifications
You must be signed in to change notification settings - Fork 42
/
template.go
50 lines (41 loc) · 1.52 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package util
import (
"bytes"
"text/template"
)
// TemplateFunctionProvider can be provided by Commands to register template functions to the internal parser.
// example: "{{ jiraTicketUrl $ticket.Key }}" can be used in custom commands which is provided by the "jiraCommand"
type TemplateFunctionProvider interface {
GetTemplateFunction() template.FuncMap
}
// Parameters is a wrapper for a map[string]string which is the default set of passing template variables
type Parameters map[string]string
var functions = template.FuncMap{
// creates a slice out of argument
"makeSlice": func(args ...any) []any {
return args
},
"slice": func(str string, start int, end int) string {
return str[start:end]
},
}
// GetTemplateFunctions returns a list of the currently available template functions which can be used in definedCommands or user specific commands
func GetTemplateFunctions() template.FuncMap {
return functions
}
// RegisterFunctions will add a function to any template renderer
func RegisterFunctions(funcMap template.FuncMap) {
for name, function := range funcMap {
functions[name] = function
}
}
// CompileTemplate pre compiles a template and returns an error if an function is not available etc
func CompileTemplate(temp string) (*template.Template, error) {
return template.New(temp).Funcs(functions).Parse(temp)
}
// EvalTemplate renders the template
func EvalTemplate(temp *template.Template, params Parameters) (string, error) {
var buf bytes.Buffer
err := temp.Execute(&buf, params)
return buf.String(), err
}