yq/cmd/compare.go

77 lines
2.3 KiB
Go
Raw Normal View History

2020-02-03 02:59:16 +00:00
package cmd
import (
"bufio"
"bytes"
2020-02-03 04:35:00 +00:00
"strings"
2020-02-03 02:59:16 +00:00
"github.com/kylelemons/godebug/diff"
2020-02-03 03:15:12 +00:00
"github.com/mikefarah/yq/v3/pkg/yqlib"
2020-02-03 02:59:16 +00:00
errors "github.com/pkg/errors"
"github.com/spf13/cobra"
)
func createCompareCmd() *cobra.Command {
var cmdCompare = &cobra.Command{
Use: "compare [yaml_file_a] [yaml_file_b]",
Aliases: []string{"x"},
2020-02-03 03:28:38 +00:00
Short: "yq x [--prettyPrint/-P] dataA.yaml dataB.yaml 'b.e(name==fr*).value'",
2020-02-03 02:59:16 +00:00
Example: `
yq x - data2.yml # reads from stdin
2020-02-03 03:28:38 +00:00
yq x -pp dataA.yaml dataB.yaml '**' # compare paths
yq x -d1 dataA.yaml dataB.yaml 'a.b.c'
2020-02-03 02:59:16 +00:00
`,
2020-02-03 03:28:38 +00:00
Long: "Deeply compares two yaml files, prints the difference. Use with prettyPrint flag to ignore formatting differences.",
2020-02-03 02:59:16 +00:00
RunE: compareDocuments,
}
2020-02-03 03:15:12 +00:00
cmdCompare.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
cmdCompare.PersistentFlags().StringVarP(&printMode, "printMode", "p", "v", "print mode (v (values, default), p (paths), pv (path and value pairs)")
cmdCompare.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
2020-02-03 02:59:16 +00:00
return cmdCompare
}
func compareDocuments(cmd *cobra.Command, args []string) error {
2020-02-03 03:15:12 +00:00
var path = ""
if len(args) < 2 {
2020-02-03 02:59:16 +00:00
return errors.New("Must provide at 2 yaml files")
2020-02-03 03:15:12 +00:00
} else if len(args) > 2 {
path = args[2]
2020-02-03 02:59:16 +00:00
}
2020-02-03 03:15:12 +00:00
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
2020-02-03 02:59:16 +00:00
if errorParsingDocIndex != nil {
return errorParsingDocIndex
}
2020-02-03 03:15:12 +00:00
var matchingNodesA []*yqlib.NodeContext
var matchingNodesB []*yqlib.NodeContext
2020-02-03 03:28:38 +00:00
var errorDoingThings error
2020-02-03 03:15:12 +00:00
2020-02-03 03:28:38 +00:00
matchingNodesA, errorDoingThings = readYamlFile(args[0], path, updateAll, docIndexInt)
2020-02-03 03:15:12 +00:00
2020-02-03 03:28:38 +00:00
if errorDoingThings != nil {
return errorDoingThings
2020-02-03 02:59:16 +00:00
}
2020-02-03 03:28:38 +00:00
matchingNodesB, errorDoingThings = readYamlFile(args[1], path, updateAll, docIndexInt)
if errorDoingThings != nil {
return errorDoingThings
2020-02-03 02:59:16 +00:00
}
var dataBufferA bytes.Buffer
var dataBufferB bytes.Buffer
2020-02-03 03:28:38 +00:00
errorDoingThings = printResults(matchingNodesA, bufio.NewWriter(&dataBufferA))
if errorDoingThings != nil {
return errorDoingThings
}
errorDoingThings = printResults(matchingNodesB, bufio.NewWriter(&dataBufferB))
if errorDoingThings != nil {
return errorDoingThings
}
2020-02-03 02:59:16 +00:00
2020-02-03 04:35:00 +00:00
cmd.Print(diff.Diff(strings.TrimSuffix(dataBufferA.String(), "\n"), strings.TrimSuffix(dataBufferB.String(), "\n")))
cmd.Print("\n")
2020-02-03 02:59:16 +00:00
return nil
}