added option for configuring the output for arrays.

This commit is contained in:
Erik Brakkee 2024-12-25 00:08:36 +01:00
parent ebd2bc41d4
commit 1adc47377f
2 changed files with 41 additions and 11 deletions

View File

@ -4,10 +4,13 @@ import (
"bytes" "bytes"
"fmt" "fmt"
yaml "github.com/goccy/go-yaml" yaml "github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"os" "os"
"reflect" "reflect"
) )
var VERBOSITY = 2
func read(file string) []byte { func read(file string) []byte {
data, err := os.ReadFile(file) data, err := os.ReadFile(file)
if err != nil { if err != nil {
@ -66,6 +69,7 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
res = append(res, item) res = append(res, item)
} }
case Type(v1) == Slice && Type(v2) == Slice: case Type(v1) == Slice && Type(v2) == Slice:
v2set := make(map[string]bool) v2set := make(map[string]bool)
v1set := make(map[string]bool) v1set := make(map[string]bool)
stringToValue := make(map[any]any) stringToValue := make(map[any]any)
@ -80,7 +84,11 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
s := make([]any, 0) s := make([]any, 0)
for k2, _ := range v2set { for k2, _ := range v2set {
if v1set[k2] { if v1set[k2] {
s = append(s, "<UNMODIFIED>") if VERBOSITY == 2 {
s = append(s, "<UNMODIFIED>")
} else if VERBOSITY == 3 {
s = append(s, stringToValue[k2])
}
} else { } else {
s = append(s, stringToValue[k2]) s = append(s, stringToValue[k2])
} }
@ -96,13 +104,12 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
return res return res
} }
func main() { func execute(cmd *cobra.Command, args []string) error {
if len(os.Args) != 3 { if len(args) != 2 {
fmt.Fprintf(os.Stderr, ` return fmt.Errorf("Parameters expected")
Usage: yamldiff <file1> <file2>") }
Shows changes and additions made in <file2> w.r.t. <file1> if VERBOSITY < 1 || VERBOSITY > 3 {
`) return fmt.Errorf("Array verbosity out of range")
os.Exit(1)
} }
file1 := os.Args[1] file1 := os.Args[1]
file2 := os.Args[2] file2 := os.Args[2]
@ -118,8 +125,23 @@ Usage: yamldiff <file1> <file2>")
//yaml.UseOrderedMap(), // Preserve map order //yaml.UseOrderedMap(), // Preserve map order
) )
err := enc.Encode(yaml2) err := enc.Encode(yaml2)
if err != nil { return err
panic(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
View File

@ -2,4 +2,12 @@ module git.wamblee.org/public/gotools
go 1.23.3 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
)