-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmerge.go
476 lines (404 loc) · 11.4 KB
/
merge.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"errors"
"fmt"
yamljson "github.com/ghodss/yaml"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-openapi/jsonpointer"
"github.com/imdario/mergo"
"github.com/mohae/deepcopy"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"time"
)
// initMap initialize a map using a bunch of keys.
func initMap(m map[string]any, keys []string) {
logDebug.Printf("(initMap) %v", keys)
if len(keys) == 0 {
return
}
if v, ok := m[keys[0]]; ok {
next := v.(map[string]any)
initMap(next, keys[1:])
return
}
m[keys[0]] = make(map[string]any)
next := m[keys[0]].(map[string]any)
initMap(next, keys[1:])
}
// Set copy the value of src defined by path into dst
// both dst and src are the entire maps
func Set(dst map[string]any, path string, src map[string]any) error {
found, srcObj, _, err := Get(src, path)
if err != nil {
return err
}
if !found {
return nil
}
pointer, err := jsonpointer.New(path)
if err != nil {
return err
}
keys := strings.Split(path, "/")
if len(keys) > 1 {
// get rid of the first key that is ""
if keys[0] == "" {
keys = keys[1:]
}
// Get rid of last key too: we don't want to initialize the last element
keys = keys[0 : len(keys)-1]
}
logDebug.Printf("(Set) keys %v", keys)
// Init the map using all the keys except the last one
initMap(dst, keys)
logDebug.Printf("(Set) map init result: %v", dst)
if _, err := pointer.Set(dst, srcObj); err != nil {
return err
}
return nil
}
// SetRelative copy the value of into dst to a specific path
// dst is the entire map
// src is only the element to copy into dst.path
func SetRelative(dst map[string]any, path string, srcObj map[string]any) error {
pointer, err := jsonpointer.New(path)
if err != nil {
return err
}
keys := strings.Split(path, "/")
if len(keys) > 1 {
// get rid of the first key that is ""
if keys[0] == "" {
keys = keys[1:]
}
// Get rid of last key too: we don't want to initialize the last element
keys = keys[0 : len(keys)-1]
}
logDebug.Printf("(Set) keys %v", keys)
// Init the map using all the keys except the last one
initMap(dst, keys)
logDebug.Printf("(Set) map init result: %v", dst)
if _, err := pointer.Set(dst, srcObj); err != nil {
return err
}
return nil
}
// Get check if a json pointer exists in a document
// First return value: true or false
// Second return value: error
func Get(doc map[string]any, str string) (bool, any, reflect.Kind, error) {
pointer, err := jsonpointer.New(str)
if err != nil {
return false, nil, 0, err
}
result, typ, err := pointer.Get(doc)
if err != nil {
if strings.HasPrefix(err.Error(), "object has no key") {
return false, result, typ, nil
}
if strings.HasPrefix(err.Error(), "object has no field") {
return false, result, typ, nil
}
if strings.HasPrefix(err.Error(), "index out of bounds") {
return false, result, typ, nil
}
return false, result, typ, err
}
return true, result, typ, nil
}
func customStrategyMerge(final map[string]any, source map[string]any, strategy MergeStrategy) error {
logDebug.Printf("customStrategyMerge(%v)", strategy)
// First deep copy source to avoid any weird behavior
src := deepcopy.Copy(source)
srcMap := src.(map[string]any)
pointer, err := jsonpointer.New(strategy.Path)
if err != nil {
return err
}
srcFound, src, srcType, srcErr := Get(srcMap, strategy.Path)
if srcErr != nil {
logErr.Fatal(srcErr)
}
dstFound, dst, dstType, dstErr := Get(final, strategy.Path)
if dstErr != nil {
logErr.Fatal(dstErr)
}
if srcFound {
if !dstFound {
if err := Set(final, strategy.Path, srcMap); err != nil {
return err
}
return nil
}
if srcType != dstType {
return fmt.Errorf("MergeStrategy error for %v: destination and src are not the same type, %v and %v", strategy, srcType, dstType)
}
// Slice
logDebug.Printf("customStrategyMerge() %v Type is %v", strategy.Path, srcType)
if srcType == reflect.Slice {
dst := dst.([]any)
src := src.([]any)
switch strategy.Strategy {
case "overwrite":
logDebug.Printf("customStrategyMerge(%v) overwrite list", strategy)
dst = src
case "merge":
logDebug.Printf("customStrategyMerge(%v) append list", strategy)
logDebug.Println("src", src)
logDebug.Println("dst", dst)
dst = append(dst, src...)
case "strategic-merge":
logDebug.Printf("customStrategyMerge(%v) strategic merge", strategy)
logDebug.Println("src", src)
logDebug.Println("dst", dst)
dst = append(dst, src...)
if dst, err = strategicCleanupSlice(dst); err != nil {
return err
}
default:
logErr.Fatal("Unknown merge strategy for list: ", strategy.Strategy)
}
if _, err := pointer.Set(final, dst); err != nil {
return err
}
return nil
}
// Map
if srcType != reflect.Map {
return fmt.Errorf("you can change merge strategy only for maps")
}
var dstPtr any
dstMap := dst.(map[string]any)
dstPtr = &dstMap
logDebug.Printf("customStrategyMerge(%v)", strategy)
switch strategy.Strategy {
case "overwrite":
if _, err := pointer.Set(final, src); err != nil {
return err
}
case "merge":
if err := mergo.Merge(
dstPtr,
src,
mergo.WithOverride,
mergo.WithOverwriteWithEmptyValue,
mergo.WithAppendSlice,
); err != nil {
return err
}
case "merge-no-append":
if err := mergo.Merge(
dstPtr,
src,
mergo.WithOverride,
mergo.WithOverwriteWithEmptyValue,
); err != nil {
return err
}
case "strategic-merge":
if err := strategicMerge(dstMap, src.(map[string]any)); err != nil {
return err
}
default:
logErr.Fatal("Unknown merge strategy ", strategy.Strategy)
}
}
return nil
}
var ErrorIncorrectMeta = errors.New("incorrect meta file")
func mergeVars(p string, mergeStrategies []MergeStrategy) (map[string]any, []Include, error) {
logDebug.Printf("mergeVars(%v)", p)
// Work with Absolute paths
if !filepath.IsAbs(p) {
if abs, errAbs := filepath.Abs(p); errAbs == nil {
p = abs
} else {
return map[string]any{}, []Include{}, errAbs
}
}
if rootFlag == "" {
rootFlag = findRoot(p)
}
mergeList, err := getMergeList(p)
if err != nil {
return map[string]any{}, []Include{}, err
}
final := make(map[string]any)
mergeListObjects := []map[string]any{}
for i := 0; i < len(mergeList); i = i + 1 {
current := make(map[string]any)
content, err := os.ReadFile(mergeList[i].path)
if err != nil {
return map[string]any{}, []Include{}, err
}
err = yamljson.Unmarshal(content, ¤t)
if err != nil {
logErr.Println("cannot unmarshal data when merging",
p,
". Error is in",
mergeList[i].path)
return map[string]any{}, []Include{}, err
}
if isMetaPath(mergeList[i].path) {
// Check if meta file has the __meta__ variable
if _, ok := current["__meta__"]; ok {
if len(current) > 1 {
logErr.Println("Meta file", mergeList[i].path,
"has __meta__ key and other variables. Please place only __meta__ in a meta file.")
return map[string]any{}, []Include{}, ErrorIncorrectMeta
}
} else {
// Inject content into the __meta__ key
newCurrent := make(map[string]any)
newCurrent["__meta__"] = current
current = newCurrent
}
}
logDebug.Println("(mergelist) append", mergeList[i])
mergeListObjects = append(mergeListObjects, current)
}
for _, current := range mergeListObjects {
// Initialization using default overwrite
for k, v := range current {
final[k] = v
}
}
logDebug.Println(mergeListObjects)
merged := make(map[string]any)
// Iterate over all the custom merge strategies and apply them in order
for _, mergeStrategy := range mergeStrategies {
mergedStrategy := make(map[string]any)
for _, current := range mergeListObjects {
if err := customStrategyMerge(mergedStrategy, current, mergeStrategy); err != nil {
logErr.Println(
"Error in custom strategy when merging",
p,
"with strategy",
mergeStrategy,
)
return map[string]any{}, []Include{}, err
}
}
// Write back result into merged, using "overwrite"
err := customStrategyMerge(
merged,
mergedStrategy,
MergeStrategy{
Path: mergeStrategy.Path,
Strategy: "overwrite",
},
)
if err != nil {
logErr.Println(
"Error in custom strategy when merging",
p,
"with strategy",
mergeStrategy,
)
return map[string]any{}, []Include{}, err
}
}
// Override final with merged vars
for k, v := range merged {
final[k] = v
}
// Add Git info to metadata
if gitFlag && isRepo(p) {
var commit *object.Commit
_, err := exec.LookPath("git")
if err != nil {
// If git is not in PATH, use pure-go
commit = findMostRecentCommit(p, extendMergeListWithRelated(p, mergeList))
} else {
// Else use git command
commit = findMostRecentCommitCmd(p, extendMergeListWithRelated(p, mergeList))
}
if commit != nil {
mergeGitInfo := map[string]any{}
mergeGitInfo["author"] = fmt.Sprintf("%s <%s>", commit.Author.Name, commit.Author.Email)
mergeGitInfo["committer"] = fmt.Sprintf("%s <%s>", commit.Committer.Name, commit.Committer.Email)
mergeGitInfo["when_author"] = commit.Author.When.UTC().Format(time.RFC3339)
mergeGitInfo["when_committer"] = commit.Committer.When.UTC().Format(time.RFC3339)
mergeGitInfo["hash"] = commit.Hash.String()
mergeGitInfo["message"] = strings.SplitN(commit.Message, "\n", 10)[0]
if err := SetRelative(final, "/__meta__/last_update/git", mergeGitInfo); err != nil {
logErr.Fatalf("Error SetRelative: %v", err)
}
}
}
// Add related file content
if config.initialized {
for _, include := range mergeList {
if !isCatalogItem(rootFlag, include.path) {
continue
}
for _, related := range config.RelatedFilesV2 {
if related.LoadInto != "" {
if related.ContentKey == "" {
logErr.Fatalf("Related file %s has no content key", related.File)
}
dir := filepath.Dir(include.path)
relatedPath := filepath.Join(dir, related.File)
if !fileExists(relatedPath) {
continue
}
content := map[string]any{}
for k, v := range related.Set {
content[k] = v
}
relatedContent, err := os.ReadFile(relatedPath)
if err != nil {
logErr.Fatalf("Error reading related file %s: %v", relatedPath, err)
}
temp := map[string]any{}
content[related.ContentKey] = string(relatedContent)
if err := SetRelative(temp, related.LoadInto, content); err != nil {
logErr.Fatalf("Error SetRelative: %v", err)
}
// Merge temp into final using mergo
if err := mergo.Merge(
&final,
temp,
mergo.WithOverride,
mergo.WithOverwriteWithEmptyValue,
mergo.WithAppendSlice,
); err != nil {
return final, mergeList, err
}
}
}
}
}
return final, mergeList, nil
}
func initMergeStrategies() {
mergeStrategies = []MergeStrategy{
{
Path: "/__meta__",
Strategy: "merge",
},
{
Path: "/agnosticv_meta",
Strategy: "merge",
},
}
if len(schemas) == 0 {
initSchemaList()
}
logDebug.Println("(INIT parse merge strategies) ")
for _, schema := range schemas {
mergeStrategies = append(mergeStrategies, schema.schema.XMerge...)
logDebug.Println("(INIT parse merge strategies) added", schema.schema.XMerge)
}
logDebug.Println("(INIT merge strategies) ", mergeStrategies)
}
func printMergeStrategies() {
fmt.Printf("# STRATEGIES:\n")
for _, mergeStrategy := range mergeStrategies {
fmt.Printf("# %-15s %s\n", mergeStrategy.Strategy, mergeStrategy.Path)
}
}