-
Notifications
You must be signed in to change notification settings - Fork 472
/
template.go
113 lines (89 loc) · 2.61 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package goss
import (
"bytes"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
)
// TemplateFilter is the type of the Goss Template Filter which include custom variables and functions.
type TemplateFilter func([]byte) ([]byte, error)
// NewTemplateFilter creates a new Template Filter based in the file and inline variables.
func NewTemplateFilter(varsFile string, varsInline string) (func([]byte) ([]byte, error), error) {
vars, err := loadVars(varsFile, varsInline)
if err != nil {
return nil, fmt.Errorf("failed while loading vars file %q: %v", varsFile, err)
}
tVars := &TmplVars{Vars: vars}
f := func(data []byte) ([]byte, error) {
t := template.New("test").Funcs(sprig.TxtFuncMap()).Funcs(funcMap)
tmpl, err := t.Parse(string(data))
if err != nil {
return []byte{}, err
}
tmpl.Option("missingkey=error")
var doc bytes.Buffer
err = tmpl.Execute(&doc, tVars)
if err != nil {
return []byte{}, err
}
return doc.Bytes(), nil
}
return f, nil
}
func mkSlice(args ...any) []any {
return args
}
func readFile(f string) (string, error) {
b, err := os.ReadFile(f)
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}
func getEnv(key string, def ...string) string {
val := os.Getenv(key)
if val == "" && len(def) > 0 {
return def[0]
}
return os.Getenv(key)
}
func regexMatch(re, s string) (bool, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return false, err
}
return compiled.MatchString(s), nil
}
// return named parenthesized subexpresions, if received, or stringfied (Sprig "get" need strings) keys like array
func findStringSubmatch(pattern, input string) map[string]interface{} {
re := regexp.MustCompile(pattern)
els := re.FindStringSubmatch(input)
elsMap := make(map[string]interface{})
elsMapNamed := make(map[string]interface{})
// create always elsMaps but returns elsMapNamed if exists named parenthesized subexps
for i := 0; i < len(els); i++ {
// convert i to string according returned (https://github.com/goss-org/goss/pull/895#issuecomment-2075716706)
elsMap[strconv.Itoa(i)] = els[i]
if re.SubexpNames()[i] != "" {
elsMapNamed[re.SubexpNames()[i]] = els[i]
}
}
// returns elsMapNamed if exists named parenthesized subexps
if len(elsMapNamed) > 0 {
return elsMapNamed
}
return elsMap
}
var funcMap = template.FuncMap{
"mkSlice": mkSlice,
"readFile": readFile,
"getEnv": getEnv,
"regexMatch": regexMatch,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"findStringSubmatch": findStringSubmatch,
}