-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrenderizer.go
More file actions
208 lines (178 loc) · 4.62 KB
/
renderizer.go
File metadata and controls
208 lines (178 loc) · 4.62 KB
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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
"github.com/gomatic/funcmap"
"github.com/imdario/mergo"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
//
func renderizer(con *cli.Context) error {
for _, config := range settings.ConfigFiles {
in, err := ioutil.ReadFile(config)
if err != nil {
if !settings.Defaulted {
return err
}
} else {
loaded := map[string]interface{}{}
err := yaml.Unmarshal(in, &loaded)
if err != nil {
return err
}
if settings.Verbose || settings.Defaulted {
log.Printf("using settings: %+v", settings.ConfigFiles)
}
loaded = retyper(loaded)
if settings.Debugging {
log.Printf("loaded: %s = %#v", config, loaded)
} else if settings.Verbose {
log.Printf("loaded: %s = %+v", config, loaded)
}
mergo.Merge(&settings.Config, loaded)
}
}
if settings.Debugging {
log.Printf("--settings:%#v", settings)
} else if settings.Verbose {
log.Printf("--settings:%+v", settings)
}
globalContext := map[string]interface{}{}
args := []string{}
// Iterate the remaining arguments for variable overrides and file names.
for a, arg := range con.Args() {
if len(arg) == 0 {
continue
} else if arg[0] != '-' {
args = append(args, arg)
continue
}
switch arg[1:][0] {
case 'c', 'C':
settings.Capitalize = !settings.Capitalize
if settings.Verbose {
log.Printf("-capitalize:%v", settings.Capitalize)
}
continue
}
currentContext := map[string]interface{}{}
nameValuePair := strings.SplitN(strings.TrimLeft(arg, "-"), "=", 2)
currentName := nameValuePair[0]
// Iterate dotted notation and construct a map from it.
var (
local = currentContext // Iterative map-reference into globalContext
leaf map[string]interface{} // Keep a reference to the leaf node
last string // Keep the leaf node's map-key
)
for _, name := range strings.Split(currentName, ".") {
if settings.Capitalize {
name = fmt.Sprintf("%s%s", strings.ToTitle(name[:1]), strings.ToLower(name[1:]))
}
local[name] = map[string]interface{}{}
leaf, last, local = local, name, local[name].(map[string]interface{})
}
var currentValue interface{}
if len(nameValuePair) == 1 { // i.e. a boolean
currentValue = []interface{}{true}
} else {
currentValue = []interface{}{typer(nameValuePair[1])}
}
leaf[last] = currentValue
if settings.Debugging {
log.Printf("index:%d name:%s value:%+v", a, currentName, currentValue)
}
if settings.Debugging {
log.Printf("currentContext: %[1]T %#[1]v", currentContext)
}
mergo.Merge(&globalContext, currentContext, mergo.WithAppendSlice)
if settings.Debugging {
log.Printf("globalContext: %[1]T %#[1]v", globalContext)
}
}
globalContext = retyper(globalContext, retypeSingleElementSlice)
// If there's no files, read from stdin.
files := args
if len(args) == 0 {
if settings.Stdin && settings.Verbose {
log.Println("source: stdin")
}
files = []string{""}
}
// Copy any loaded keys into the globalContext unless they already exist, i.e. they were provided on the command line.
mergo.Merge(&globalContext, settings.Config)
if settings.Environment != "" || len(args) == 0 {
v := make(map[string]string)
for _, item := range os.Environ() {
splits := strings.Split(item, "=")
v[splits[0]] = strings.Join(splits[1:], "=")
}
globalContext[settings.Environment] = v
}
// Dump the settings
if settings.Debugging {
log.Printf("globalContext: %#v", globalContext)
} else if settings.Verbose {
if o, err := yaml.Marshal(globalContext); err != nil {
log.Printf("globalContext: %+v", globalContext)
} else {
log.Printf("globalContext:\n%s", o)
}
}
// Execute each template
status := 0
for _, file := range files {
status |= func() (status int) {
defer func() {
if r := recover(); r != nil {
log.Printf("PANIC: %+v", r)
status = 15
}
}()
var err error
var data []byte
var r io.ReadCloser
if file == "" {
r = os.Stdin
} else {
r, err = os.Open(file)
if err != nil {
log.Println(err)
return 1
}
defer r.Close()
}
f, err := ioutil.ReadAll(r)
if err != nil {
log.Println(err)
return 2
}
data = f
tmpl, err := template.New(file).
Option(fmt.Sprintf("missingkey=%s", settings.MissingKey)).
Funcs(funcmap.Map).
Parse(string(data))
if err != nil {
log.Print(err)
return 4
}
var b bytes.Buffer
err = tmpl.Execute(&b, globalContext)
if err != nil {
log.Print(err)
return 8
}
data = b.Bytes()
fmt.Println(string(data))
return 0
}()
}
os.Exit(status)
return nil
}