can now also merge resources and do a symmetric diff.
This commit is contained in:
parent
f52507aa8f
commit
56844a3c24
@ -4,19 +4,50 @@ import (
|
|||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MyMap yaml.MapSlice
|
||||||
|
|
||||||
|
func (m *MyMap) Set(key any, value any) {
|
||||||
|
for i := range len(*m) {
|
||||||
|
if (*m)[i].Key == key {
|
||||||
|
(*m)[i].Value = value
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*m = append(*m, yaml.MapItem{Key: key, Value: value})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MyMap) Get(key any) any {
|
||||||
|
for _, item := range m {
|
||||||
|
if item.Key == key {
|
||||||
|
return item.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func mergeMap(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) yaml.MapSlice {
|
func mergeMap(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) yaml.MapSlice {
|
||||||
res := yaml1
|
res := MyMap(yaml1)
|
||||||
|
|
||||||
//for key, item := range yaml2 {
|
for _, item := range yaml2 {
|
||||||
// switch {
|
initialValue := res.Get(item.Key)
|
||||||
// case res.ToMap()[key] != nil && type
|
value := item.Value
|
||||||
//
|
switch {
|
||||||
// }
|
case initialValue != nil:
|
||||||
//}
|
if reflect.TypeOf(initialValue) == reflect.TypeOf(yaml.MapSlice{}) &&
|
||||||
|
reflect.TypeOf(value) == reflect.TypeOf(yaml.MapSlice{}) {
|
||||||
return res
|
mergedMap := mergeMap(initialValue.(yaml.MapSlice), value.(yaml.MapSlice))
|
||||||
|
res.Set(item.Key, mergedMap)
|
||||||
|
} else {
|
||||||
|
res.Set(item.Key, item.Value)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
res.Set(item.Key, item.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return yaml.MapSlice(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func merge(cmd *cobra.Command, args []string) error {
|
func merge(cmd *cobra.Command, args []string) error {
|
||||||
|
@ -57,7 +57,7 @@ Shows the additions and modifications in <file2> compared to <file1>`,
|
|||||||
}
|
}
|
||||||
cmd.AddCommand(merge)
|
cmd.AddCommand(merge)
|
||||||
|
|
||||||
cmd.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level",
|
diff.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level",
|
||||||
"v", 3, `Array output level: ,
|
"v", 3, `Array output level: ,
|
||||||
0: no output, only exit status,
|
0: no output, only exit status,
|
||||||
1: only show changed/added values,
|
1: only show changed/added values,
|
||||||
|
Loading…
Reference in New Issue
Block a user