-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
76 lines (64 loc) · 2.07 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"strings"
"text/template"
"github.com/Masterminds/sprig"
)
// FuncMap returns a mapping of all of the functions that Engine has.
//
// Because some functions are late-bound (e.g. contain context-sensitive
// data), the functions may not all perform identically outside of an
// Engine as they will inside of an Engine.
//
// Known late-bound functions:
//
// - "include": This is late-bound in Engine.Render(). The version
// included in the FuncMap is a placeholder.
// - "required": This is late-bound in Engine.Render(). The version
// included in the FuncMap is a placeholder.
// - "tpl": This is late-bound in Engine.Render(). The version
// included in the FuncMap is a placeholder.
func FuncMap() template.FuncMap {
f := sprig.TxtFuncMap()
delete(f, "env")
delete(f, "expandenv")
// Add some extra functionality
extra := template.FuncMap{
"indent2": indent2,
"getFile": getFile,
"getTextfile": getTextfile,
"multiline": multilineYaml,
"contains": contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replace": replace,
"toToml": ToToml,
"toYaml": ToYaml,
"fromYaml": FromYaml,
"toJson": ToJson,
"fromJson": FromJson,
// This is a placeholder for the "include" function, which is
// late-bound to a template. By declaring it here, we preserve the
// integrity of the linter.
// "include": func(string, interface{}) string { return "not implemented" },
// "required": func(string, interface{}) interface{} { return "not implemented" },
// "tpl": func(string, interface{}) interface{} { return "not implemented" },
}
for k, v := range extra {
f[k] = v
}
return f
}
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func indent2(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return strings.Replace(v, "\n", "\n"+pad, -1)
}
func multilineYaml(v string) string {
return "|\n" + v
}
func replace(from string, to string, occurances int, str string) string {
return strings.Replace(str, from, to, occurances)
}