-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (102 loc) · 2.94 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"text/template"
"gopkg.in/yaml.v2"
)
func main() {
// input params
runTimeEnv := flag.String("r", "local", "env: local, k8s")
targetTemplate := flag.String("ta", "", "yaml format: configmap, common")
templateFile := flag.String("t", "", "path to template file")
dataFilePath := flag.String("f", "", "path to data file")
outputFilePath := flag.String("o", "", "path to output file")
flag.Parse()
// params check
if *runTimeEnv == "" || *templateFile == "" || *dataFilePath == "" || *outputFilePath == "" {
flag.Usage()
log.Println("example:")
log.Println("easyconf -r local -ta common -t template.yaml -f data.yaml -o generateFile.yaml")
return
}
// read template file
templateContent, err := os.ReadFile(*templateFile)
if err != nil {
log.Println("Failed to read template file:", err)
return
}
data := parseValue(dataFilePath)
dataPart, ok := data[*runTimeEnv]
if !ok {
fmt.Println("no such env files", err)
return
}
if *targetTemplate == "configmap" {
// gen configmap.yaml
err = generateFile(templateContent, dataPart, *outputFilePath)
if err != nil {
fmt.Println("Failed to write output file:", err)
return
}
} else {
// local read configmap.yaml data with multi files
local(templateContent, dataPart, *outputFilePath)
}
}
type ConfigMap struct {
Data map[string]string "yaml:`data`"
}
// local env
func local(templateContent []byte, dataMap map[string]string, outputFilePath string) {
// parse yaml file
var configMap ConfigMap
err := yaml.Unmarshal(templateContent, &configMap)
if err != nil {
log.Println("parse configmap.yaml error:", err)
return
}
for fname, value := range configMap.Data {
err := generateFile([]byte(value), dataMap, outputFilePath+fname)
if err != nil {
log.Println(err)
}
}
}
// parse template and combine data
func generateFile(templateContent []byte, dataMap map[string]string, outputFilePath string) error {
// parse template
tmpl := template.Must(template.New("yaml").Parse(string(templateContent)))
// create generateFile file
output, err := os.Create(outputFilePath)
if err != nil {
return errors.New(fmt.Sprintln("Failed to create generateFile file:", err))
}
defer output.Close()
// Render the data using a template and write the results to an output file
err = tmpl.Execute(output, dataMap)
if err != nil {
return errors.New(fmt.Sprintln("Failed to execute generateFile file:", err))
}
log.Println("generate success:", outputFilePath)
return nil
}
// parseValue parse configuration data file
func parseValue(dataFilePath *string) map[string]map[string]string {
// read content
dataContent, err := os.ReadFile(*dataFilePath)
if err != nil {
log.Println("Failed to read data file:", err)
return nil
}
// parse YAML data
var dataMap map[string]map[string]string
err = yaml.Unmarshal(dataContent, &dataMap)
if err != nil {
log.Fatal("Failed to unmarshal YAML data:", err)
}
return dataMap
}