Skip to content

Commit c7b0f54

Browse files
authored
Update main.go
1 parent 5dd22b8 commit c7b0f54

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
)
1414

1515
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)")
1920
)
2021

2122
// Init initializes the logger and parses command-line flags.
@@ -41,7 +42,7 @@ func main() {
4142
log.Fatal().Err(err).Msgf("Failed to parse YAML from file: %s", file)
4243
}
4344

44-
result, err = Merge(result, parsedData)
45+
result, err = Merge(result, parsedData, *mergeStrategy)
4546
if err != nil {
4647
log.Fatal().Err(err).Msgf("Failed to merge YAML content from file: %s", file)
4748
}
@@ -73,9 +74,9 @@ func main() {
7374
}
7475
}
7576

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)
7980
switch typedA := a.(type) {
8081
case []interface{}:
8182
typedB, ok := b.([]interface{})
@@ -93,10 +94,10 @@ func Merge(a, b interface{}) (interface{}, error) {
9394
continue
9495
}
9596
leftVal, found := typedA[key]
96-
if !found {
97+
if !found || strategy == "override" {
9798
typedA[key] = rightVal
9899
} else {
99-
mergedVal, err := Merge(leftVal, rightVal)
100+
mergedVal, err := Merge(leftVal, rightVal, strategy)
100101
if err != nil {
101102
return nil, err
102103
}

0 commit comments

Comments
 (0)