-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (74 loc) · 2.07 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
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"github.com/locustbaby/stt/utils"
)
func main() {
// Define command-line flags
valuesFile := flag.String("v", "", "Values file")
templateFile := flag.String("t", "", "Template file or directory path")
outputDir := flag.String("o", "", "Output Directory path, only [Dir]")
delimiter := flag.String("d", "{{,}}", "delimiter, like \"[[,]]\"")
// Parse command-line flags
flag.Parse()
// Check if required flags are provided
if *templateFile == "" {
flag.PrintDefaults()
return
}
// Read values.yaml file
values, err := utils.ReadFile(*valuesFile)
if err != nil {
utils.HandleError("Error reading", *valuesFile, err)
return
}
// Parse values.yaml file and convert its content to a Go map
valuesMap, err := utils.ParseYAML(values)
if err != nil {
utils.HandleError("Error parsing", *valuesFile, err)
return
}
// Create the output directory
if *outputDir != "" {
err = utils.CreateDirectory(*outputDir)
if err != nil {
utils.HandleError("Error creating", *outputDir, err)
return
}
}
// Traverse all files in the templates directory
err = filepath.Walk(*templateFile, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore directories
if info.IsDir() {
return nil
}
// Read the template file
templateContent, err := utils.ReadFile(path)
if err != nil {
utils.HandleError("Error reading template", path, err)
return err
}
// Render the template
outputContent, err := utils.RenderTemplate(string(templateContent), valuesMap, strings.Split(*delimiter, ",")[0], strings.Split(*delimiter, ",")[1])
if err != nil {
utils.HandleError("Error rendering template", path, err)
return err
}
// Write to file
if *outputDir != "" {
err = utils.WriteFile(filepath.Join(*outputDir, info.Name()), outputContent)
if err != nil {
utils.HandleError("Error writing to", filepath.Join(*outputDir, info.Name()), err)
return err
}
}
return nil
})
utils.HandleError("Error processing templates", *templateFile, err)
}