-
Notifications
You must be signed in to change notification settings - Fork 586
/
yamlUtil.go
254 lines (211 loc) · 6.5 KB
/
yamlUtil.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package yaml
import (
"bytes"
"fmt"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"gopkg.in/yaml.v2"
"io"
"os"
"reflect"
"regexp"
"strings"
)
type fUtils interface {
FileRead(name string) ([]byte, error)
FileWrite(name string, data []byte, mode os.FileMode) error
}
var _fileUtils fUtils
var _stat = os.Stat
var _traverse = traverse
// Substitute ...
func Substitute(ymlFile string, replacements map[string]interface{}, replacementsFiles []string) (bool, error) {
if _fileUtils == nil {
_fileUtils = piperutils.Files{}
}
bIn, err := _fileUtils.FileRead(ymlFile)
if err != nil {
return false, err
}
inDecoder := yaml.NewDecoder(bytes.NewReader(bIn))
buf := new(bytes.Buffer)
outEncoder := yaml.NewEncoder(buf)
var updated bool
mergedReplacements, err := getReplacements(replacements, replacementsFiles)
if err != nil {
return false, err
}
for {
mIn := make(map[string]interface{})
decodeErr := inDecoder.Decode(&mIn)
if decodeErr != nil {
if decodeErr == io.EOF {
break
}
return false, decodeErr
}
if err != nil {
return false, err
}
out, _updated, err := _traverse(mIn, mergedReplacements)
if err != nil {
return false, err
}
updated = _updated || updated
err = outEncoder.Encode(out)
}
if updated {
fInfo, err := _stat(ymlFile)
if err != nil {
return false, err
}
err = _fileUtils.FileWrite(ymlFile, buf.Bytes(), fInfo.Mode())
if err != nil {
return false, err
}
}
return updated, nil
}
func traverse(node interface{}, replacements map[string]interface{}) (interface{}, bool, error) {
switch t := node.(type) {
case string:
return handleString(t, replacements)
case bool:
return t, false, nil
case int:
return t, false, nil
case map[string]interface{}:
return handleMap(t, replacements)
case map[interface{}]interface{}:
m, err := keysToString(t)
if err != nil {
return nil, false, err
}
return handleMap(m, replacements)
case []interface{}:
return handleSlice(t, replacements)
default:
return nil, false, fmt.Errorf("Unknown type received: '%v' (%v)", reflect.TypeOf(node), node)
}
}
func keysToString(m map[interface{}]interface{}) (map[string]interface{}, error) {
result := map[string]interface{}{}
for key, val := range m {
if k, ok := key.(string); ok {
result[k] = val
} else {
return nil, fmt.Errorf("Cannot downcast'%v' to string. Type: %v)", reflect.TypeOf(key), key)
}
}
return result, nil
}
func handleString(value string, replacements map[string]interface{}) (interface{}, bool, error) {
trimmed := strings.TrimSpace(value)
re := regexp.MustCompile(`\(\(.*?\)\)`)
matches := re.FindAllSubmatch([]byte(trimmed), -1)
fullMatch := isFullMatch(trimmed, matches)
if fullMatch {
log.Entry().Infof("FullMatchFound: %v", value)
parameterName := getParameterName(matches[0][0])
parameterValue := getParameterValue(parameterName, replacements)
if parameterValue == nil {
return nil, false, fmt.Errorf("No value available for parameters '%s', replacements: %v", parameterName, replacements)
}
log.Entry().Infof("FullMatchFound: '%s', replacing with '%v'", parameterName, parameterValue)
return parameterValue, true, nil
}
// we have to scan for multiple variables
// we return always a string
updated := false
for i, match := range matches {
parameterName := getParameterName(match[0])
log.Entry().Infof("XPartial match found: (%d) %v, %v", i, parameterName, value)
parameterValue := getParameterValue(parameterName, replacements)
if parameterValue == nil {
return nil, false, fmt.Errorf("No value available for parameter '%s', replacements: %v", parameterName, replacements)
}
var conversion string
switch t := parameterValue.(type) {
case string:
conversion = "%s"
case bool:
conversion = "%t"
case int:
conversion = "%d"
case float64:
conversion = "%g" // exponent as need, only required digits
default:
return nil, false, fmt.Errorf("Unsupported datatype found during travseral of yaml file: '%v', type: '%v'", parameterValue, reflect.TypeOf(t))
}
valueAsString := fmt.Sprintf(conversion, parameterValue)
log.Entry().Infof("Value as String: %v: '%v'", parameterName, valueAsString)
value = strings.Replace(value, "(("+parameterName+"))", valueAsString, -1)
updated = true
log.Entry().Infof("PartialMatchFound (%d): '%v', replaced with : '%s'", i, parameterName, valueAsString)
}
return value, updated, nil
}
func getParameterName(b []byte) string {
pName := string(b)
log.Entry().Infof("ParameterName is: '%s'", pName)
return strings.Replace(strings.Replace(string(b), "((", "", 1), "))", "", 1)
}
func getParameterValue(name string, replacements map[string]interface{}) interface{} {
r := replacements[name]
log.Entry().Infof("Value '%v' resolved for parameter '%s'", r, name)
return r
}
func isFullMatch(value string, matches [][][]byte) bool {
return strings.HasPrefix(value, "((") && strings.HasSuffix(value, "))") && len(matches) == 1 && len(matches[0]) == 1
}
func handleSlice(t []interface{}, replacements map[string]interface{}) ([]interface{}, bool, error) {
tNode := make([]interface{}, 0)
updated := false
for _, e := range t {
if val, _updated, err := traverse(e, replacements); err == nil {
updated = updated || _updated
tNode = append(tNode, val)
} else {
return nil, false, err
}
}
return tNode, updated, nil
}
func handleMap(t map[string]interface{}, replacements map[string]interface{}) (map[string]interface{}, bool, error) {
tNode := make(map[string]interface{})
updated := false
for key, value := range t {
if val, _updated, err := traverse(value, replacements); err == nil {
updated = updated || _updated
tNode[key] = val
} else {
return nil, false, err
}
}
return tNode, updated, nil
}
func getReplacements(replacements map[string]interface{}, replacementsFiles []string) (map[string]interface{}, error) {
mReplacements := make(map[string]interface{})
for _, replacementsFile := range replacementsFiles {
bReplacements, err := _fileUtils.FileRead(replacementsFile)
if err != nil {
return nil, err
}
replacementsDecoder := yaml.NewDecoder(bytes.NewReader(bReplacements))
for {
decodeErr := replacementsDecoder.Decode(&mReplacements)
if decodeErr != nil {
if decodeErr == io.EOF {
break
}
return nil, decodeErr
}
}
}
// the parameters from the map has a higher precedence,
// hence we merge after resolving parameters from the files
for k, v := range replacements {
mReplacements[k] = v
}
return mReplacements, nil
}