@@ -13,9 +13,10 @@ import (
13
13
)
14
14
15
15
var (
16
- ignoreList = pflag .StringSlice ("ignore" , []string {}, "keys to ignore when merging" )
17
- outputFile = pflag .String ("output" , "" , "output file to write the merged YAML (optional)" )
18
- dryRun = pflag .Bool ("dry-run" , false , "perform a dry run, showing what changes would be made without applying them" )
16
+ ignoreList = pflag .StringSlice ("ignore" , []string {}, "keys to ignore when merging" )
17
+ outputFile = pflag .String ("output" , "" , "output file to write the merged YAML (optional)" )
18
+ dryRun = pflag .Bool ("dry-run" , false , "perform a dry run, showing what changes would be made without applying them" )
19
+ mergeStrategy = pflag .String ("merge-strategy" , "merge" , "specify merge strategy: 'merge' (deep merge) or 'override' (override conflicting keys)" )
19
20
)
20
21
21
22
// Init initializes the logger and parses command-line flags.
@@ -41,7 +42,7 @@ func main() {
41
42
log .Fatal ().Err (err ).Msgf ("Failed to parse YAML from file: %s" , file )
42
43
}
43
44
44
- result , err = Merge (result , parsedData )
45
+ result , err = Merge (result , parsedData , * mergeStrategy )
45
46
if err != nil {
46
47
log .Fatal ().Err (err ).Msgf ("Failed to merge YAML content from file: %s" , file )
47
48
}
@@ -73,9 +74,9 @@ func main() {
73
74
}
74
75
}
75
76
76
- // Merge recursively merges two YAML structures.
77
- func Merge (a , b interface {}) (interface {}, error ) {
78
- log .Debug ().Msgf ("Merging: %v (%T) with %v (%T)" , a , a , b , b )
77
+ // Merge recursively merges or overrides two YAML structures based on the strategy .
78
+ func Merge (a , b interface {}, strategy string ) (interface {}, error ) {
79
+ log .Debug ().Msgf ("Merging with strategy '%s' : %v (%T) with %v (%T)" , strategy , a , a , b , b )
79
80
switch typedA := a .(type ) {
80
81
case []interface {}:
81
82
typedB , ok := b .([]interface {})
@@ -93,10 +94,10 @@ func Merge(a, b interface{}) (interface{}, error) {
93
94
continue
94
95
}
95
96
leftVal , found := typedA [key ]
96
- if ! found {
97
+ if ! found || strategy == "override" {
97
98
typedA [key ] = rightVal
98
99
} else {
99
- mergedVal , err := Merge (leftVal , rightVal )
100
+ mergedVal , err := Merge (leftVal , rightVal , strategy )
100
101
if err != nil {
101
102
return nil , err
102
103
}
0 commit comments