-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
198 lines (168 loc) · 3.83 KB
/
funcs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig/v3"
"gopkg.in/yaml.v3"
)
var inputsDir = "."
func getFuncMap() template.FuncMap {
f := sprig.GenericFuncMap()
f["fromInputDir"] = fromInputDir
f["include"] = include
f["toBool"] = toBool
f["toToml"] = toToml
f["toYaml"] = toYaml
f["absPath"] = absPath
f["fileExists"] = fileExists
f["fileMode"] = fileMode
f["fileMtime"] = fileMtime
f["fileRead"] = fileRead
f["fileSize"] = fileSize
f["isDir"] = isDir
f["isFile"] = isFile
f["joinPath"] = joinPath
f["toBackslash"] = toBackslash
f["toOsPath"] = toOsPath
f["toSlash"] = toSlash
return f
}
// --- Includes ------------------------------------------------------------------------------------
func include(input string) (string, error) {
var err error
includeDir := inputsDir
if !filepath.IsAbs(input) {
includeDir, err = getIncludeDir(input)
if err != nil {
return "", err
}
}
input = filepath.Join(includeDir, input)
outputString, err := executeTemplateFile(input)
if err != nil {
return "", err
}
return outputString, nil
}
func fromInputDir(input string) (string, error) {
dir, err := getIncludeDir(input)
if err != nil {
return "", err
}
return filepath.Join(dir, input), nil
}
func getIncludeDir(input string) (string, error) {
if input == "-" {
cwd, err := filepath.Abs(".")
if err != nil {
return "", err
}
return cwd, nil
} else {
return inputsDir, nil
}
}
// --- Type conversion -----------------------------------------------------------------------------
func toBool(value string) (bool, error) {
result, err := strconv.ParseBool(value)
if err != nil {
return false, err
}
return result, nil
}
func toToml(v interface{}) (string, error) {
buf := bytes.NewBuffer(nil)
enc := toml.NewEncoder(buf)
err := enc.Encode(v)
if err != nil {
return "", err
}
return buf.String(), nil
}
func toYaml(v interface{}) (string, error) {
data, err := yaml.Marshal(v)
if err != nil {
return "", err
}
return string(data), nil
}
// --- Paths ---------------------------------------------------------------------------------------
func absPath(file string) (string, error) {
new_file, err := filepath.Abs(file)
if err != nil {
return "", err
}
return new_file, nil
}
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, err
}
return true, nil
}
func fileMode(path string) (string, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return "", err
}
return info.Mode().String(), nil
}
func fileMtime(file string) (string, error) {
info, err := os.Stat(file)
if err != nil {
return "", err
}
return info.ModTime().String(), nil
}
func fileRead(file string) (string, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(data), nil
}
func fileSize(file string) (int64, error) {
info, err := os.Stat(file)
if err != nil {
return 0, err
}
return info.Size(), nil
}
func isDir(path string) (bool, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false, err
}
return info.IsDir(), nil
}
func isFile(path string) (bool, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false, err
}
return info.Mode().IsRegular(), nil
}
func joinPath(segments []any) string {
string_segments := make([]string, 0, len(segments))
for _, v := range segments {
string_segments = append(string_segments, fmt.Sprint(v))
}
return filepath.Join(string_segments...)
}
func toBackslash(path string) string {
return strings.ReplaceAll(path, "/", "\\")
}
func toOsPath(path string) string {
return strings.ReplaceAll(path, "\\", string(filepath.Separator))
}
func toSlash(path string) string {
return strings.ReplaceAll(path, "\\", "/")
}