forked from kenjones-cisco/dinamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
145 lines (125 loc) · 3.26 KB
/
generate.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
package generator
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
var (
// SupportedFileTypes provides list of file extensions supported for data files
SupportedFileTypes = []string{".json", ".yaml", ".yml"}
dataFs = afero.NewOsFs()
)
// DataSources specifies different sources of data
type DataSources struct {
Data []string
DataFile string
UseEnv bool
}
// Generate file using the specified template file and input data to the specified file location.
func Generate(inputTemplate, outfile string, sources *DataSources) error {
data := make(map[string]interface{})
// handle the sources using the inverse priority (file --> env --> args)
if sources.DataFile != "" {
ext, err := fileType(sources.DataFile)
if err != nil {
return err
}
b, err := afero.ReadFile(dataFs, fileAbs(sources.DataFile))
if err != nil {
return err
}
fileData, err := fileMap(ext, b)
if err != nil {
return err
}
updateMap(fileData, data)
log.Debugf("(fileData) data: %v", data)
}
if sources.UseEnv {
envData := listMap(os.Environ())
updateMap(envData, data)
log.Debugf("(envData) data: %v", data)
}
if len(sources.Data) > 0 {
argsData := listMap(sources.Data)
updateMap(argsData, data)
log.Debugf("(argsData) data: %v", data)
}
return generate(inputTemplate, outfile, data)
}
func listMap(list []string) map[string]interface{} {
amap := make(map[string]interface{})
for _, item := range list {
kv := strings.SplitN(item, "=", 2)
amap[kv[0]] = kv[1]
}
return amap
}
func fileMap(ext string, data []byte) (map[string]interface{}, error) {
amap := make(map[string]interface{})
switch ext {
case ".yaml", ".yml":
if err := yaml.Unmarshal(data, &amap); err != nil {
return nil, err
}
case ".json":
if err := json.Unmarshal(data, &amap); err != nil {
return nil, err
}
}
return amap, nil
}
func updateMap(src, dest map[string]interface{}) {
for k, v := range src {
if v != nil {
dest[k] = v
}
}
}
func fileType(file string) (string, error) {
ext := filepath.Ext(file)
for _, item := range SupportedFileTypes {
if item == ext {
return ext, nil
}
}
return "", fmt.Errorf("unsupported file type: %s\nonly %q supported", file, SupportedFileTypes)
}
func fileAbs(file string) string {
// only in very rare scenarios will `filepath.Abs` return an error
// based on the code in 1.10 only if getting the current working directory
// fails. If that happens there are likely much larger problems. Therefore
// just return the same value provided.
f, err := filepath.Abs(file)
if err != nil {
return file
}
return f
}
func generate(inputTemplate, outfile string, data map[string]interface{}) error {
log.WithFields(log.Fields{
"template": inputTemplate,
"file": outfile,
}).Debugf("generating file with data: %v", data)
t := fileAbs(inputTemplate)
// Load template
tmpl, err := template.New(filepath.Base(t)).Funcs(sprig.TxtFuncMap()).ParseFiles(t)
if err != nil {
return err
}
tmpl.Option("missingkey=error")
f, err := dataFs.Create(fileAbs(outfile))
if err != nil {
return err
}
defer f.Close()
// generate the file
return tmpl.Execute(f, data)
}