diff --git a/cmd/yamldiff/yamldiff.go b/cmd/yamldiff/yamldiff.go index 3d3b21c..42c12e4 100644 --- a/cmd/yamldiff/yamldiff.go +++ b/cmd/yamldiff/yamldiff.go @@ -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] { - s = append(s, "") + if VERBOSITY == 2 { + s = append(s, "") + } 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 ") - Shows changes and additions made in w.r.t. -`) - 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 ") //yaml.UseOrderedMap(), // Preserve map order ) err := enc.Encode(yaml2) - if err != nil { - panic(err) + return err +} + +func main() { + + cmd := &cobra.Command{ + Use: "yamldiff ", + Short: "Shows one-way difference between yaml files", + Long: ` +Shows the changes in compared to `, + 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 , 3: show all values") + + cmd.Execute() } diff --git a/go.mod b/go.mod index 9212ccf..53b3ae7 100644 --- a/go.mod +++ b/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 +)