forked from byung2/tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpl.go
678 lines (639 loc) · 17.4 KB
/
tpl.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package tpl
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/imdario/mergo"
"github.com/spf13/viper"
ini "github.com/vaughan0/go-ini"
"gopkg.in/yaml.v2"
)
// TmplOpts holds options to execute template
type TmplOpts struct {
DataFilesStr string
DataFiles []string
DataFormat string
TmplFiles []string
Output string
OutDir string
OutExt string
MissingKey string
Interactive bool
UseEnv bool
UseEnvFromPrefix string
DataOutFile string
DataOutFormat string
FoldContext bool
ShowProcessedFile bool
ShowOnlyMissingKey bool
Overwrite bool
}
// Tmpl contains metadata
type Tmpl struct {
Re *regexp.Regexp
TmplOpts *TmplOpts
Data map[string]interface{}
Files []*TmplFileMeta
}
// TmplFileMeta holds information about template file
type TmplFileMeta struct {
Name string
OrigPath string
DestPath string
Mode os.FileMode
Content string
Changed bool
}
// LineMeta holds metadata specific to the line
type LineMeta struct {
Line string
ReplacedLine string
Space int
NoValue bool
}
const (
missingKeyError = "missingkey=error"
missingKeyDefault = "missingkey=default"
)
func getFileExt(file string) string {
return strings.TrimPrefix(filepath.Ext(file), ".")
}
// OptsToTmpl creates a Tmpl object from TmplOpts
func (opts *TmplOpts) OptsToTmpl() (Tmpl, error) {
tmpl := Tmpl{TmplOpts: opts}
// Check options
missingKey := strings.ToLower(opts.MissingKey)
if missingKey == "" {
missingKey = "error"
}
if missingKey != "error" && missingKey != "zero" && missingKey != "default" && missingKey != "invalid" {
return tmpl, fmt.Errorf("wrong missing key option")
}
opts.MissingKey = missingKey
useEnv := viper.Get("tpl.env")
foldContext := viper.Get("tpl.fold-context")
interactive := viper.Get("tpl.interactive")
overwrite := viper.Get("tpl.overwrite")
showProcessedFile := viper.Get("tpl.show-processed-info")
if useEnv != nil && !opts.UseEnv {
opts.UseEnv = viper.GetBool("tpl.env")
}
if foldContext != nil && !opts.FoldContext {
opts.FoldContext = viper.GetBool("tpl.fold-context")
}
if interactive != nil && !opts.Interactive {
opts.Interactive = viper.GetBool("tpl.interactive")
}
if overwrite != nil && !opts.Overwrite {
opts.Overwrite = viper.GetBool("tpl.overwrite")
}
if showProcessedFile != nil && !opts.ShowProcessedFile {
opts.ShowProcessedFile = viper.GetBool("tpl.show-processed-info")
}
// data files separator: space vs colon
//opts.DataFiles = strings.Fields(opts.DataFilesStr)
dataFilesElem := make(map[string]bool)
dataFiles := []string{}
if opts.DataFilesStr != "" {
dataFiles = strings.Split(opts.DataFilesStr, ":")
}
for _, v := range dataFiles {
matches, err := filepath.Glob(v)
if err != nil {
return tmpl, fmt.Errorf("datafile glob error: %v", err)
}
for _, match := range matches {
_, ok := dataFilesElem[match]
if !ok {
dataFilesElem[match] = true
}
}
}
for k := range dataFilesElem {
opts.DataFiles = append(opts.DataFiles, k)
}
defaultDataFormat := "yaml"
switch opts.DataFormat {
case "json", "yml", "yaml", "ini", "kv":
defaultDataFormat = opts.DataFormat
}
// DataFiles to Data object
datakv := make(map[string]interface{})
for _, file := range opts.DataFiles {
dat, err := ioutil.ReadFile(file)
if err != nil {
return tmpl, err
}
//var kv map[string]interface{}
kv := make(map[string]interface{})
ext := getFileExt(file)
switch ext {
case "json", "yml", "yaml", "ini", "kv":
default:
ext = defaultDataFormat
}
switch ext {
case "yml", "yaml":
err = yaml.Unmarshal(dat, &kv)
if err != nil {
return tmpl, fmt.Errorf("failed to parse yaml data file '%s': %v", file, err)
}
case "json":
err = json.Unmarshal(dat, &kv)
if err != nil {
return tmpl, fmt.Errorf("failed to parse json data file '%s': %v", file, err)
}
case "ini":
inifile, err := ini.Load(bytes.NewReader(dat))
if err != nil {
return tmpl, fmt.Errorf("failed to parse ini data file '%s': %v", file, err)
}
for name, section := range inifile {
kv[name] = section
}
case "kv":
// parse key=value format
tmpKv := make(map[string]interface{})
scanner := bufio.NewScanner(strings.NewReader(string(dat)))
for scanner.Scan() {
line := scanner.Text()
elems := strings.Split(line, "=")
if len(elems) <= 1 {
fmt.Printf("warn: datafile is neither yaml nor key=value format\n")
continue
}
tmpKv[appendKeyPrefix(elems[0])] = strings.TrimSpace(elems[1])
}
kv = expand(tmpKv)
}
mergo.Merge(&datakv, kv)
}
tmpl.Data = datakv
tmpl.TmplOpts = opts
tmpl.Re = regexp.MustCompile(`{{[\s]*(\..*?)[\s]*}}`)
if opts.UseEnv || opts.UseEnvFromPrefix != "" {
tmpDataOutFormat := tmpl.TmplOpts.DataOutFormat
tmpl.TmplOpts.DataOutFormat = "kv"
keysStr, err := tmpl.ExtractKeys()
tmpl.TmplOpts.DataOutFormat = tmpDataOutFormat
if err != nil {
return tmpl, err
}
keyScanner := bufio.NewScanner(strings.NewReader(keysStr))
keys := make(map[string]bool)
for keyScanner.Scan() {
keys[strings.SplitN(keyScanner.Text(), "=", 2)[0]] = true
//keys[appendKeyPrefix(strings.SplitN(keyScanner.Text(), "=", 2)[0])] = true
}
envPrefix := opts.UseEnvFromPrefix
if opts.UseEnvFromPrefix != "" {
envPrefix = opts.UseEnvFromPrefix + "."
}
envs := os.Environ()
if envPrefix == "" {
for _, e := range envs {
elems := strings.Split(e, "=")
if len(elems) >= 1 {
_, ok := datakv[elems[0]]
if !ok {
_, ok = keys[elems[0]]
if ok {
datakv[elems[0]] = elems[1]
}
}
}
}
} else {
for _, e := range envs {
elems := strings.Split(e, "=")
if len(elems) >= 1 {
_, ok := keys[envPrefix+elems[0]]
if ok {
if datakv[opts.UseEnvFromPrefix] == nil {
datakv[opts.UseEnvFromPrefix] = make(map[interface{}]interface{})
}
i, ok := datakv[opts.UseEnvFromPrefix].(map[interface{}]interface{})
if ok {
_, ok = i[elems[0]]
if !ok {
i[elems[0]] = elems[1]
}
} else {
i2, ok := datakv[opts.UseEnvFromPrefix].(map[string]interface{})
if ok {
_, ok = i2[elems[0]]
if !ok {
i2[elems[0]] = elems[1]
}
} else {
return tmpl, fmt.Errorf("env prefix is already used for data key")
}
}
}
}
}
}
}
return tmpl, nil
}
func appendKeyPrefix(text string) string {
if strings.HasPrefix(text, ".") {
return text
}
return "." + text
}
func trimKeyPrefix(text string) string {
return strings.TrimPrefix(text, ".")
}
// Ensure no missing keys in the template file
func (tmpl *Tmpl) Ensure(file string) error {
data := tmpl.Data
t, err := template.ParseFiles(file)
if err != nil {
return fmt.Errorf("error parsing template(s): %v", err)
}
// check missing keys
t.Option(missingKeyError)
buf := new(bytes.Buffer)
err = t.Execute(buf, data)
if err != nil {
if !strings.Contains(err.Error(), "map has no entry for key") {
return fmt.Errorf("failed to execute template: %v", err)
}
return fmt.Errorf("missing key found: %v", err)
}
return nil
}
// Keys store all missing keys to dataFlattenMap
func (tmpl *Tmpl) Keys(file string, dataFlattenMap map[string]interface{}) error {
re := tmpl.Re
// file to LineMeta
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer f.Close()
var lines []string
reader := bufio.NewReader(f)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSuffix(line, "\n")
lines = append(lines, line)
}
for _, line := range lines {
subMatched := re.FindAllStringSubmatch(line, -1)
for _, x := range subMatched {
_, ok := dataFlattenMap[x[1]]
if !ok {
dataFlattenMap[x[1]] = ""
}
}
}
return nil
}
// Execute gotemplate
func (tmpl *Tmpl) Execute(file string, dataFlattenMap map[string]interface{}) error {
re := tmpl.Re
data := tmpl.Data
opts := tmpl.TmplOpts
interactive := opts.Interactive
//outDir := opts.OutDir
t, err := template.ParseFiles(file)
if err != nil {
return fmt.Errorf("error parsing template(s): %v", err)
}
fileinfo, err := os.Stat(file)
tfm := &TmplFileMeta{
Name: path.Base(file),
OrigPath: file,
Mode: fileinfo.Mode(),
//Changed: ooo,
}
t.Option(fmt.Sprintf("missingkey=%s", opts.MissingKey))
buf := new(bytes.Buffer)
err = t.Execute(buf, data)
if err != nil {
if !strings.Contains(err.Error(), "map has no entry for key") {
return fmt.Errorf("failed to execute template: %v", err)
}
if !interactive {
return fmt.Errorf("interactive flag is disabled, but the missing key is found: %v", err)
}
} else {
tfm.Content = buf.String()
tmpl.Files = append(tmpl.Files, tfm)
//continue
return nil
}
// file to LineMeta
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer f.Close()
lineMetaList := []*LineMeta{}
reader := bufio.NewReader(f)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSuffix(line, "\n")
space := countLeadingSpace(line)
lineMeta := LineMeta{Line: line, Space: space}
lineMetaList = append(lineMetaList, &lineMeta)
}
// execute template with different missingkey option
t.Option(missingKeyDefault)
renderedOutputBuf := new(bytes.Buffer)
err = t.Execute(renderedOutputBuf, data)
if err != nil {
return fmt.Errorf("failed to execute template with default option '%s': %v", missingKeyDefault, err)
}
reader = bufio.NewReader(strings.NewReader(renderedOutputBuf.String()))
i := 0
for {
line, err := reader.ReadString('\n')
// TODO EOF
if err != nil {
break
}
line = strings.TrimSuffix(line, "\n")
if i >= len(lineMetaList) {
fmt.Println("failed to interaction: i > len(lineMetaList)", i, len(lineMetaList))
}
if strings.Contains(line, "<no value>") {
lineMetaList[i].ReplacedLine = line
lineMetaList[i].NoValue = true
}
i = i + 1
}
c := InitializedNavColorMeta()
c.NavFile.Printf("[%s]\n", file)
for i, lineMeta := range lineMetaList {
if lineMeta.NoValue {
prevSpace := lineMetaList[i].Space
tmpLineMetaList := []LineMeta{}
for j := i - 1; j >= 0; j-- {
space := lineMetaList[j].Space
if lineMetaList[j].Line != "" && lineMetaList[j].Line != "\n" && space < prevSpace {
tmpLineMeta := LineMeta{Line: lineMetaList[j].Line}
tmpLineMetaList = append(tmpLineMetaList, tmpLineMeta)
prevSpace = space
if space == 0 {
break
}
}
}
ttListLen := len(tmpLineMetaList)
line := lineMetaList[i].Line
subMatched := re.FindAllStringSubmatch(line, -1)
// do not show context if a key already has value
needToInput := false
for _, x := range subMatched {
_, ok := dataFlattenMap[x[1]]
if !ok {
needToInput = true
break
} else {
}
}
if !needToInput {
continue
}
if !opts.FoldContext {
//fmt.Printf("\n")
c.NavTitle.Printf("missing key found\n")
for k := ttListLen - 1; k >= 0; k-- {
tmpLineMeta := tmpLineMetaList[k]
c.NavContext.Printf("%s\n", tmpLineMeta.Line)
}
c.NavContext.Printf("%s\n", lineMetaList[i].Line)
}
for _, x := range subMatched {
_, ok := dataFlattenMap[x[1]]
if !ok {
c.NavInput.Printf("value for '%s': ", x[1])
val := getValueStdin()
dataFlattenMap[x[1]] = val
}
}
if !opts.FoldContext {
fmt.Printf("\n")
}
}
}
newNestedDataMap := expand(dataFlattenMap)
t.Option(fmt.Sprintf("missingkey=%s", opts.MissingKey))
// execute template with new data
buf = new(bytes.Buffer)
err = t.Execute(buf, newNestedDataMap)
if err != nil {
//TODO
return fmt.Errorf("failed to execute template: %v", err)
}
tfm.Content = buf.String()
tmpl.Files = append(tmpl.Files, tfm)
return nil
}
// ExtractKeys get all missing keys and processed key:value pairs with given format (by default, yaml)
func (tmpl *Tmpl) ExtractKeys() (string, error) {
tmpMissingKeyOption := tmpl.TmplOpts.MissingKey
str, err := tmpl.extractKeys()
tmpl.TmplOpts.MissingKey = tmpMissingKeyOption
return str, err
}
func (tmpl Tmpl) extractKeys() (string, error) {
tmpl.TmplOpts.MissingKey = "default"
tmplFiles := tmpl.TmplOpts.TmplFiles
dataFlattenMap := make(map[string]interface{})
givenDataFlattenMap := make(map[string]interface{})
data := tmpl.Data
//fmt.Println("DEBUG: tmpl.Data:", data)
nestedToFlattenMap(data, givenDataFlattenMap, "", false)
for _, file := range tmplFiles {
err := tmpl.Execute(file, givenDataFlattenMap)
if err != nil {
return "", err
}
}
for _, file := range tmplFiles {
err := tmpl.Keys(file, dataFlattenMap)
if err != nil {
return "", err
}
}
// fill values with the given data
for key := range dataFlattenMap {
value, ok := givenDataFlattenMap[key]
if ok {
dataFlattenMap[key] = value
}
}
if tmpl.TmplOpts.ShowOnlyMissingKey {
for key := range givenDataFlattenMap {
_, ok := dataFlattenMap[key]
if ok {
delete(dataFlattenMap, key)
}
}
}
dataOut, err := tmpl.marshalData(dataFlattenMap)
return dataOut, err
}
func (tmpl Tmpl) marshalData(dataFlattenMap map[string]interface{}) (string, error) {
expandedKeys := expand(dataFlattenMap)
dataOut := ""
if len(dataFlattenMap) > 0 {
if tmpl.TmplOpts.DataOutFormat == "" {
ext := getFileExt(tmpl.TmplOpts.DataOutFile)
switch ext {
case "json", "yml", "yaml", "ini", "kv":
tmpl.TmplOpts.DataOutFormat = ext
}
}
switch tmpl.TmplOpts.DataOutFormat {
case "kv":
for key, value := range dataFlattenMap {
dataOut = fmt.Sprintf("%s%s=%v\n", dataOut, trimKeyPrefix(key), value)
}
case "json":
dd, err := json.MarshalIndent(&expandedKeys, "", " ")
if err != nil {
return "", fmt.Errorf("failed to marshal keys map to json: %v", err)
}
dataOut = fmt.Sprintf("%s\n", string(dd))
default:
dd, err := yaml.Marshal(&expandedKeys)
if err != nil {
return "", fmt.Errorf("failed to marshal keys map to yaml: %v", err)
}
dataOut = fmt.Sprintf("---\n\n%s", string(dd))
//dataOut = fmt.Sprintf("%s", string(dd))
}
}
return dataOut, nil
}
// WriteDataObject writes the filled data to the output
func (tmpl *Tmpl) WriteDataObject(dataOut string) {
if tmpl.TmplOpts.DataOutFile != "" {
WriteStringToFileAndCreateDir(tmpl.TmplOpts.DataOutFile, dataOut, tmpl.TmplOpts.Overwrite)
} else {
fmt.Printf("%s", dataOut)
}
}
// ExecuteFiles executes template files with datafile
// and writes the filled data to the file if the file is specified
func (tmpl *Tmpl) ExecuteFiles() error {
data := tmpl.Data
dataFlattenMap := make(map[string]interface{})
nestedToFlattenMap(data, dataFlattenMap, "", false)
tmplFiles := tmpl.TmplOpts.TmplFiles
for _, file := range tmplFiles {
err := tmpl.Execute(file, dataFlattenMap)
if err != nil {
return err
}
}
if tmpl.TmplOpts.DataOutFile != "" {
dataOut, err := tmpl.marshalData(dataFlattenMap)
if err != nil {
return err
}
WriteStringToFileAndCreateDir(tmpl.TmplOpts.DataOutFile, dataOut, tmpl.TmplOpts.Overwrite)
}
return nil
}
// EnsureFiles check for missing keys in the template files
func (tmpl *Tmpl) EnsureFiles() error {
tmplFiles := tmpl.TmplOpts.TmplFiles
for _, file := range tmplFiles {
err := tmpl.Ensure(file)
if err != nil {
return err
}
}
return nil
}
// FillDestPath fill in the values for destination path
func (tmpl *Tmpl) FillDestPath(trimPrefixForDestPath string) {
output := tmpl.TmplOpts.Output
outdir := tmpl.TmplOpts.OutDir
//ignoreDirOfOrigPath := tmpl.TmplOpts.IgnoreDirOfOrigPath
if outdir == "" && output == "" {
return
}
isMultipleTemplates := false
if len(tmpl.Files) > 1 {
isMultipleTemplates = true
}
for _, tmplMeta := range tmpl.Files {
if outdir != "" {
origPath := tmplMeta.OrigPath
if trimPrefixForDestPath != "" {
origPath = strings.TrimPrefix(origPath, trimPrefixForDestPath)
}
//if ignoreDirOfOrigPath {
// origPath = tmplMeta.Name
//}
if strings.HasSuffix(origPath, ".tpl") {
origPath = strings.TrimSuffix(origPath, ".tpl")
} else if strings.HasSuffix(origPath, ".tmpl") {
origPath = strings.TrimSuffix(origPath, ".tmpl")
}
if output != "" && !isMultipleTemplates {
origPath = output
}
tmplMeta.DestPath = outdir + string(os.PathSeparator) + origPath
} else {
tmplMeta.DestPath = output
}
}
}
// WriteProcessedTmpl writes processed template
func (tmpl *Tmpl) WriteProcessedTmpl() error {
output := tmpl.TmplOpts.Output
outdir := tmpl.TmplOpts.OutDir
lenTmplFiles := len(tmpl.Files)
if lenTmplFiles > 1 && output != "" && outdir == "" {
return fmt.Errorf("multiple template files given with empty 'outdir' flag and non-empty 'out' flag")
}
c := InitializedNavColorMeta()
for idx, tmplMeta := range tmpl.Files {
if outdir == "" && output == "" {
if idx > 0 {
fmt.Printf("\n")
}
if tmpl.TmplOpts.ShowProcessedFile { // lenTmplFiles > 1 &&
c.ExecInfo.Printf("'%s' is processed\n", tmplMeta.OrigPath)
}
fmt.Printf("%s", tmplMeta.Content)
continue
}
err := WriteStringToFileAndCreateDir(tmplMeta.DestPath, tmplMeta.Content, tmpl.TmplOpts.Overwrite)
//err := WriteStringToFileWithModeAndCreateDir(tmplMeta.DestPath, tmplMeta.Content, tmplMeta.Mode)
if err != nil {
switch err.(type) {
case *ErrFileExists:
continue
default:
return err
}
}
if tmpl.TmplOpts.ShowProcessedFile {
c.ExecInfo.Printf("'%s' is processed and stored in '%s'\n", tmplMeta.OrigPath, tmplMeta.DestPath)
}
}
return nil
}