gotools/cmd/yamltool/yamltool.go

71 lines
1.5 KiB
Go
Raw Normal View History

2025-01-07 08:27:12 +00:00
package main
import (
yaml "github.com/goccy/go-yaml"
"github.com/spf13/cobra"
)
var VERBOSITY = 3
var SYMMETRIC_DIFF = false
type TypeId int
const (
Map TypeId = iota
Slice
Scalar
)
func Type(elem any) TypeId {
switch elem.(type) {
case yaml.MapSlice:
return Map
case []any:
return Slice
default:
return Scalar
}
}
func main() {
cmd := &cobra.Command{
Use: "yamltool",
Short: "Shows one-way difference between yaml files",
Long: `
Shows the changes in <file2> compared to <file1>`,
}
diff := &cobra.Command{
Use: "diff [file1] [file2]",
Short: "Shows one-way difference between yaml files",
Long: `
Shows the additions and modifications in <file2> compared to <file1>`,
RunE: func(cmd *cobra.Command, args []string) error {
return diff(cmd, args)
},
}
cmd.AddCommand(diff)
merge := &cobra.Command{
Use: "merge [file1] ... [fileN]",
Short: "Merge yaml files.",
Long: `Changes will be merged into the first file, so later files override earlier ones`,
RunE: func(cmd *cobra.Command, args []string) error {
return merge(cmd, args)
},
}
cmd.AddCommand(merge)
cmd.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level",
"v", 3, `Array output level: ,
0: no output, only exit status,
1: only show changed/added values,
2: show identical as <UNMODIFIED>,
3: show all values`)
diff.Flags().BoolVarP(&SYMMETRIC_DIFF, "symmetric-diff",
"s", false, `Symmetric difference, compare in both directions`)
cmd.Execute()
}