added option for configuring the output for arrays.
This commit is contained in:
parent
ebd2bc41d4
commit
1adc47377f
@ -4,10 +4,13 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
yaml "github.com/goccy/go-yaml"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var VERBOSITY = 2
|
||||
|
||||
func read(file string) []byte {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
@ -66,6 +69,7 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
|
||||
res = append(res, item)
|
||||
}
|
||||
case Type(v1) == Slice && Type(v2) == Slice:
|
||||
|
||||
v2set := make(map[string]bool)
|
||||
v1set := make(map[string]bool)
|
||||
stringToValue := make(map[any]any)
|
||||
@ -80,7 +84,11 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
|
||||
s := make([]any, 0)
|
||||
for k2, _ := range v2set {
|
||||
if v1set[k2] {
|
||||
if VERBOSITY == 2 {
|
||||
s = append(s, "<UNMODIFIED>")
|
||||
} else if VERBOSITY == 3 {
|
||||
s = append(s, stringToValue[k2])
|
||||
}
|
||||
} else {
|
||||
s = append(s, stringToValue[k2])
|
||||
}
|
||||
@ -96,13 +104,12 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
|
||||
return res
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, `
|
||||
Usage: yamldiff <file1> <file2>")
|
||||
Shows changes and additions made in <file2> w.r.t. <file1>
|
||||
`)
|
||||
os.Exit(1)
|
||||
func execute(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("Parameters expected")
|
||||
}
|
||||
if VERBOSITY < 1 || VERBOSITY > 3 {
|
||||
return fmt.Errorf("Array verbosity out of range")
|
||||
}
|
||||
file1 := os.Args[1]
|
||||
file2 := os.Args[2]
|
||||
@ -118,8 +125,23 @@ Usage: yamldiff <file1> <file2>")
|
||||
//yaml.UseOrderedMap(), // Preserve map order
|
||||
)
|
||||
err := enc.Encode(yaml2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "yamldiff <file1> <file2>",
|
||||
Short: "Shows one-way difference between yaml files",
|
||||
Long: `
|
||||
Shows the changes in <file2> compared to <file1>`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return execute(cmd, args)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level",
|
||||
"v", 1, "Array output level: , 1: only show changed/added values, 2 (default) show identical as <UNMODIFIED>, 3: show all values")
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
|
10
go.mod
10
go.mod
@ -2,4 +2,12 @@ module git.wamblee.org/public/gotools
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require github.com/goccy/go-yaml v1.15.13
|
||||
require (
|
||||
github.com/goccy/go-yaml v1.15.13
|
||||
github.com/spf13/cobra v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user