/
setcfg.go
207 lines (167 loc) · 4.82 KB
/
setcfg.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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"reflect"
"regexp"
"strings"
"gopkg.in/yaml.v2"
)
// Initialise a default pattern that finds placeholders that are wrapped in tilde characters.
// For example: ~hello~
var placeholderPattern = regexp.MustCompile("~(.*?)~")
// A type to hold any number of input arguments to support ad-hoc
type flagStrings []string
func (fs *flagStrings) String() string {
return strings.Join(*fs, ",")
}
func (fs *flagStrings) Set(value string) error {
*fs = append(*fs, value)
return nil
}
func main() {
input := flag.String("i", "", "Absolute or relative path to input YAML file.")
env := flag.String("e", "", "Absolute or relative path to the environment YAML file.")
pattern := flag.String("pattern", "~(.*?)~", "The regex pattern to use for extracting keys.")
adhoc := &flagStrings{}
flag.Var(adhoc, "f", "A list of 'key=value' fields to substitute (useful as an alternative to -e if all you're substituting are simple fields).")
flag.Parse()
if *input == "" {
flag.Usage()
os.Exit(2)
}
placeholderPattern = regexp.MustCompile(*pattern)
inputFile, err := os.Open(*input)
if err != nil {
log.Fatalf("error reading input file: %v", err)
}
envParsed := map[interface{}]interface{}{}
if *env != "" {
envFile, err := os.Open(*env)
if err != nil {
log.Fatalf("error reading input environment file: %v", err)
}
if envParsed, err = parse(envFile); err != nil {
log.Fatalf("error unmarshalling environment file: %v", err)
}
}
if err := addAdhocFields(envParsed, adhoc); err != nil {
log.Fatalf("error setting adhoc fields: %v", err)
}
outputs := parseFile(inputFile, envParsed)
fmt.Println(outputs)
}
func parseFile(input io.Reader, env map[interface{}]interface{}) string {
decoder := yaml.NewDecoder(input)
outputs := []string{}
for {
var inputParsed map[interface{}]interface{}
if err := decoder.Decode(&inputParsed); err != nil {
if err == io.EOF {
break
}
log.Fatalf("reading yaml part in file: %v", err)
}
if err := setParsed(inputParsed, env); err != nil {
log.Fatalf("error setting file: %v", err)
}
output, err := yaml.Marshal(inputParsed)
if err != nil {
log.Fatalf("error marshalling output: %v", err)
}
outputs = append(outputs, (string(output)))
}
return strings.Join(outputs, "---\n")
}
func parse(input io.Reader) (map[interface{}]interface{}, error) {
var parsed map[interface{}]interface{}
err := yaml.NewDecoder(input).Decode(&parsed)
// If there aren't any environment fields, just return an empty collection.
if err == io.EOF {
return map[interface{}]interface{}{}, nil
}
return parsed, err
}
func addAdhocFields(envParsed map[interface{}]interface{}, adhoc *flagStrings) error {
if adhoc == nil {
return nil
}
for _, field := range *adhoc {
k, v, err := parseAdhocKeyValue(field)
if err != nil {
return err
}
envParsed[k] = v
}
return nil
}
func parseAdhocKeyValue(field string) (string, string, error) {
parts := strings.SplitN(field, "=", 2)
if len(parts) < 2 {
return "", "", fmt.Errorf("adhoc fields must be in the format of key=value")
}
return parts[0], parts[1], nil
}
func setParsed(inputParsed, envParsed map[interface{}]interface{}) error {
for k, v := range inputParsed {
if v == nil {
continue
}
switch reflect.TypeOf(v).Kind() {
// Recurse into complex fields.
case reflect.Map:
if err := setParsed(v.(map[interface{}]interface{}), envParsed); err != nil {
return err
}
// Set complex and scalar fields.
case reflect.Slice:
x := reflect.ValueOf(v)
for i := 0; i < x.Len(); i++ {
switch reflect.TypeOf(x.Index(i).Interface()).Kind() {
case reflect.Slice:
log.Println("implement support for slice of slices")
case reflect.Map:
if err := setParsed(x.Index(i).Interface().(map[interface{}]interface{}), envParsed); err != nil {
return err
}
case reflect.String:
if err := setValue(v, v, inputParsed, envParsed); err != nil {
return err
}
}
}
// Set scalar field.
default:
if err := setValue(k, v, inputParsed, envParsed); err != nil {
return err
}
}
}
return nil
}
func isPlaceholder(value interface{}) (string, bool) {
valueStr, ok := value.(string)
if !ok {
return "", false
}
matches := placeholderPattern.FindStringSubmatch(valueStr)
if len(matches) < 2 {
return "", false
}
return matches[1], true
}
func setValue(k, v interface{}, inputParsed, envParsed map[interface{}]interface{}) error {
key, ok := isPlaceholder(v)
if !ok {
return nil
}
part, ok := envParsed[key]
if !ok {
return fmt.Errorf("missing part for %q", v)
}
inputParsed[k] = part
return nil
}