-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
224 lines (194 loc) · 4.52 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strings"
"text/template"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/ghodss/yaml"
)
var (
buildtime string
gitcommit string
appversion string
templateFile string
yamlFile string
suffix string
outputdir string
rematch = `\$\{?([a-zA-Z0-9_]*)\}?`
envVarMatch *regexp.Regexp
)
func main() {
log.Printf("appversion: %s", appversion)
log.Printf("gitcommit: %s", gitcommit)
log.Printf("buildtime: %s", buildtime)
app := kingpin.New("yaml-renderer", "Render templates with yaml variable input.")
app.Flag("templates", "path to the template file or directory").
Short('t').
StringVar(&templateFile)
app.Flag("yaml", "path to the yaml value file").
Short('y').
StringVar(&yamlFile)
app.Flag("output", "path to the output direcory (will be created if not exists)").
Short('o').
Default("rendered").
StringVar(&outputdir)
envVarMatch = regexp.MustCompile(rematch)
kingpin.MustParse(app.Parse(os.Args[1:]))
info, err := os.Stat(templateFile)
if err != nil {
panic(err)
}
if outputdir == templateFile {
panic("output directory must not be template directory")
}
if info.IsDir() {
files, err := ioutil.ReadDir(templateFile)
if err != nil {
panic(err)
}
for _, f := range files {
i, err := os.Stat(templateFile + "/" + f.Name())
if i.IsDir() {
continue
}
err = renderFile(templateFile + "/" + f.Name())
if err != nil {
panic(err)
}
}
} else {
err := renderFile(templateFile)
if err != nil {
panic(err)
}
}
}
func renderFile(inputfilename string) error {
inputfilebasename := path.Base(inputfilename)
if len(outputdir) < 1 {
outputdir = "."
}
outputFileName := outputdir + "/" + inputfilebasename
if _, err := os.Stat(outputdir); os.IsNotExist(err) {
err := os.MkdirAll(outputdir, 0755)
if err != nil {
return err
}
}
outputFile, err := os.OpenFile(outputFileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if err != nil {
return err
}
defer outputFile.Close()
yamlFileContent, err := ioutil.ReadFile(yamlFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var yamlData map[string]interface{}
err = yaml.Unmarshal(yamlFileContent, &yamlData)
if err != nil {
return err
}
getEnvVarForMapValue(&yamlData)
log.Printf("using template file %s", inputfilename)
content, err := ioutil.ReadFile(inputfilename)
if err != nil {
return err
}
t := template.Must(template.New(inputfilename).Funcs(template.FuncMap{"N": N}).Parse(string(content)))
var renderedBytes bytes.Buffer
err = t.Execute(&renderedBytes, yamlData)
if err != nil {
return err
}
log.Printf("writing file %s", outputFileName)
_, err = outputFile.Write(renderedBytes.Bytes())
if err != nil {
return err
}
return nil
}
func getEnvVarForMapValue(indata interface{}) {
data := *indata.(*map[string]interface{})
for key, value := range *indata.(*map[string]interface{}) {
if v, ok := value.([]interface{}); ok {
var list []interface{}
for _, s := range v {
if elm, ok := s.(string); ok {
// we need to regexp match the string and iterate over the matches here
matches := envVarMatch.FindAllStringSubmatch(elm, -1)
for _, m := range matches {
if len(m) < 2 {
if elm, ok := s.(string); ok {
if len(elm) > 0 {
list = append(list, elm)
}
}
continue
}
envvar := os.Getenv(m[1])
if len(envvar) > 0 {
for _, ie := range strings.Split(envvar, ",") {
if len(ie) > 0 {
list = append(list, ie)
}
}
}
}
} else {
list = append(list, s)
}
}
if len(list) > 0 {
data[key] = list
} else {
data[key] = value
}
continue
}
if s, ok := value.(string); ok {
// we need to regexp match the string and iterate over the matches here
matches := envVarMatch.FindAllStringSubmatch(s, -1)
envvarval := s
for _, m := range matches {
if len(m) > 1 {
envvar := os.Getenv(m[1])
if len(envvar) > 0 {
envvarval = strings.Replace(envvarval, m[0], envvar, -1)
}
}
}
data[key] = envvarval
continue
}
if v, ok := value.(interface{}); ok {
if d, ok := v.(map[string]interface{}); ok {
getEnvVarForMapValue(&d)
}
}
}
}
func N(end interface{}) (stream chan int) {
e := 0
switch end.(type) {
case float64:
e = int(end.(float64))
case int:
e = end.(int)
}
stream = make(chan int)
go func() {
for i := 0; i < e; i++ {
stream <- i
}
close(stream)
}()
return
}