Skip to content

Commit

Permalink
Update main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyanio authored Sep 22, 2024
1 parent 5dd22b8 commit c7b0f54
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

var (
ignoreList = pflag.StringSlice("ignore", []string{}, "keys to ignore when merging")
outputFile = pflag.String("output", "", "output file to write the merged YAML (optional)")
dryRun = pflag.Bool("dry-run", false, "perform a dry run, showing what changes would be made without applying them")
ignoreList = pflag.StringSlice("ignore", []string{}, "keys to ignore when merging")
outputFile = pflag.String("output", "", "output file to write the merged YAML (optional)")
dryRun = pflag.Bool("dry-run", false, "perform a dry run, showing what changes would be made without applying them")
mergeStrategy = pflag.String("merge-strategy", "merge", "specify merge strategy: 'merge' (deep merge) or 'override' (override conflicting keys)")
)

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

result, err = Merge(result, parsedData)
result, err = Merge(result, parsedData, *mergeStrategy)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to merge YAML content from file: %s", file)
}
Expand Down Expand Up @@ -73,9 +74,9 @@ func main() {
}
}

// Merge recursively merges two YAML structures.
func Merge(a, b interface{}) (interface{}, error) {
log.Debug().Msgf("Merging: %v (%T) with %v (%T)", a, a, b, b)
// Merge recursively merges or overrides two YAML structures based on the strategy.
func Merge(a, b interface{}, strategy string) (interface{}, error) {
log.Debug().Msgf("Merging with strategy '%s': %v (%T) with %v (%T)", strategy, a, a, b, b)
switch typedA := a.(type) {
case []interface{}:
typedB, ok := b.([]interface{})
Expand All @@ -93,10 +94,10 @@ func Merge(a, b interface{}) (interface{}, error) {
continue
}
leftVal, found := typedA[key]
if !found {
if !found || strategy == "override" {
typedA[key] = rightVal
} else {
mergedVal, err := Merge(leftVal, rightVal)
mergedVal, err := Merge(leftVal, rightVal, strategy)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c7b0f54

Please sign in to comment.