-
Notifications
You must be signed in to change notification settings - Fork 4
/
template.go
149 lines (129 loc) · 3.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Ultimate Provisioner: UP cmd
// Copyright (c) 2019 Stephen Cheng and contributors
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package impl
import (
"bytes"
"github.com/Masterminds/sprig/v3"
u "github.com/upcmd/up/utils"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/leekchan/gtf"
"text/template"
)
var (
templateFuncs template.FuncMap
)
func FuncMapInit() {
taskFuncs := template.FuncMap{
"OS": func() string { return runtime.GOOS },
"ARCH": func() string { return runtime.GOARCH },
"catLines": func(s string) string {
s = strings.Replace(s, "\r\n", " ", -1)
return strings.Replace(s, "\n", " ", -1)
},
"splitLines": func(s string) []string {
s = strings.Replace(s, "\r\n", "\n", -1)
return strings.Split(s, "\n")
},
"fromSlash": func(path string) string {
return filepath.FromSlash(path)
},
"toSlash": func(path string) string {
return filepath.ToSlash(path)
},
//--------------------------------------------------------
"now": func() string {
t := time.Now()
return t.Format("2006-01-02T15:04:05+11:00")
},
"printobj": func(obj interface{}) string {
u.Ppmsg(obj)
return u.Sppmsg(obj)
},
//reg do not return any value, so do not expect the dvar value will be something other than empty
"reg": func(varname string, object interface{}) string {
TaskRuntime().ExecbaseVars.Put(varname, object)
return ""
},
"dereg": func(varname string) string {
TaskRuntime().ExecbaseVars.Delete(varname)
return ""
},
"filecontent": func(filepath string) string {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
u.LogWarn("filecontent readfile", u.Spf("please fix file path: %s", filepath))
return ""
} else {
content, err := ioutil.ReadFile(filepath)
if err != nil {
u.LogWarn("filecontent readfile", u.Spf("please fix file read error, path: %s", filepath))
}
return string(content)
}
},
"validateMandatoryFailIfNone": func(varname, varvalue string) string {
if varvalue == "" {
u.InvalidAndExit("validateMandatoryFailIfNone", u.Spf("Required var:(%s) must not be empty, please fix it", varname))
}
return varvalue
},
}
templateFuncs = sprig.TxtFuncMap()
for k, v := range gtf.GtfTextFuncMap {
if _, ok := templateFuncs[k]; !ok {
//does not exit, add it to funcmap now
templateFuncs[k] = v
}
}
for k, v := range taskFuncs {
templateFuncs[k] = v
}
}
func ToJson(str string) string {
return Render("{{toJson .}}", str)
}
func Render(tstr string, obj interface{}) string {
tname := "."
t, err := template.New(tname).Funcs(templateFuncs).Parse(tstr)
u.LogErrorAndExit("template creating", err, "Please fix the template issue and try again")
var result bytes.Buffer
err = t.Execute(&result, obj)
u.LogErrorAndContinue("template rendering problem", err, u.PrintContentWithLineNuber(tstr))
return result.String()
}
func ListAllFuncs() {
var builtins = map[string]string{
"and": "and",
"call": "call",
"html": "HTMLEscaper",
"index": "index",
"js": "JSEscaper",
"len": "length",
"not": "not",
"or": "or",
"print": "fmt.Sprint",
"printf": "fmt.Sprintf",
"println": "fmt.Sprintln",
"urlquery": "URLQueryEscaper",
// Comparisons
"eq": "eq", // ==
"ge": "ge", // >=
"gt": "gt", // >
"le": "le", // <=
"lt": "lt", // <
"ne": "ne", // !=
}
for k, v := range builtins {
u.Pf("%30s : %#v\n", k, v)
}
for k, v := range templateFuncs {
u.Pf("%30s : %#v\n", k, v)
}
}