mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 07:08:06 +00:00
More powerful compare
This commit is contained in:
parent
9e47685271
commit
d828b214cc
@ -5,67 +5,67 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"github.com/kylelemons/godebug/diff"
|
"github.com/kylelemons/godebug/diff"
|
||||||
|
"github.com/mikefarah/yq/v3/pkg/yqlib"
|
||||||
errors "github.com/pkg/errors"
|
errors "github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
yaml "gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func createCompareCmd() *cobra.Command {
|
func createCompareCmd() *cobra.Command {
|
||||||
var cmdCompare = &cobra.Command{
|
var cmdCompare = &cobra.Command{
|
||||||
Use: "compare [yaml_file_a] [yaml_file_b]",
|
Use: "compare [yaml_file_a] [yaml_file_b]",
|
||||||
Aliases: []string{"x"},
|
Aliases: []string{"x"},
|
||||||
Short: "yq x data1.yml data2.yml",
|
Short: "yq x data1.yml data2.yml ",
|
||||||
Example: `
|
Example: `
|
||||||
yq x - data2.yml # reads from stdin
|
yq x - data2.yml # reads from stdin
|
||||||
`,
|
`,
|
||||||
Long: "Compares two yaml files, prints the difference",
|
Long: "Compares two yaml files, prints the difference. same ",
|
||||||
RunE: compareDocuments,
|
RunE: compareDocuments,
|
||||||
}
|
}
|
||||||
cmdCompare.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based)")
|
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().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print (does not have an affect with json output)")
|
cmdCompare.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print (does not have an affect with json output)")
|
||||||
|
cmdCompare.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
|
||||||
return cmdCompare
|
return cmdCompare
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareDocuments(cmd *cobra.Command, args []string) error {
|
func compareDocuments(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) != 2 {
|
var path = ""
|
||||||
|
|
||||||
|
if len(args) < 2 {
|
||||||
return errors.New("Must provide at 2 yaml files")
|
return errors.New("Must provide at 2 yaml files")
|
||||||
}
|
} else if len(args) > 2 {
|
||||||
if docIndex == "*" {
|
path = args[2]
|
||||||
return errors.New("Document splat for compare not yet supported")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex()
|
||||||
if errorParsingDocIndex != nil {
|
if errorParsingDocIndex != nil {
|
||||||
return errorParsingDocIndex
|
return errorParsingDocIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataBucketA yaml.Node
|
var matchingNodesA []*yqlib.NodeContext
|
||||||
var dataBucketB yaml.Node
|
var matchingNodesB []*yqlib.NodeContext
|
||||||
var errorReadingStream error
|
var errorReadingStream error
|
||||||
errorReadingStream = readData(args[0], docIndexInt, &dataBucketA)
|
|
||||||
|
matchingNodesA, errorReadingStream = readYamlFile(args[0], path, updateAll, docIndexInt)
|
||||||
|
|
||||||
if errorReadingStream != nil {
|
if errorReadingStream != nil {
|
||||||
return errorReadingStream
|
return errorReadingStream
|
||||||
}
|
}
|
||||||
|
|
||||||
errorReadingStream = readData(args[1], docIndexInt, &dataBucketB)
|
matchingNodesB, errorReadingStream = readYamlFile(args[1], path, updateAll, docIndexInt)
|
||||||
if errorReadingStream != nil {
|
if errorReadingStream != nil {
|
||||||
return errorReadingStream
|
return errorReadingStream
|
||||||
}
|
}
|
||||||
|
|
||||||
if prettyPrint {
|
if prettyPrint {
|
||||||
updateStyleOfNode(&dataBucketA, 0)
|
setStyle(matchingNodesA, 0)
|
||||||
updateStyleOfNode(&dataBucketB, 0)
|
setStyle(matchingNodesB, 0)
|
||||||
}
|
|
||||||
|
|
||||||
if errorReadingStream != nil {
|
|
||||||
return errorReadingStream
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataBufferA bytes.Buffer
|
var dataBufferA bytes.Buffer
|
||||||
printNode(&dataBucketA, bufio.NewWriter(&dataBufferA))
|
|
||||||
|
|
||||||
var dataBufferB bytes.Buffer
|
var dataBufferB bytes.Buffer
|
||||||
printNode(&dataBucketB, bufio.NewWriter(&dataBufferB))
|
printResults(matchingNodesA, bufio.NewWriter(&dataBufferA))
|
||||||
|
printResults(matchingNodesB, bufio.NewWriter(&dataBufferB))
|
||||||
|
|
||||||
cmd.Print(diff.Diff(dataBufferA.String(), dataBufferB.String()))
|
cmd.Print(diff.Diff(dataBufferA.String(), dataBufferB.String()))
|
||||||
return nil
|
return nil
|
||||||
|
@ -54,5 +54,5 @@ func readProperty(cmd *cobra.Command, args []string) error {
|
|||||||
setStyle(matchingNodes, 0)
|
setStyle(matchingNodes, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return printResults(matchingNodes, cmd)
|
return printResults(matchingNodes, cmd.OutOrStdout())
|
||||||
}
|
}
|
||||||
|
31
cmd/utils.go
31
cmd/utils.go
@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
"github.com/mikefarah/yq/v3/pkg/yqlib"
|
"github.com/mikefarah/yq/v3/pkg/yqlib"
|
||||||
errors "github.com/pkg/errors"
|
errors "github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
yaml "gopkg.in/yaml.v3"
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -66,23 +65,20 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml.
|
|||||||
return append(originalMatchingNodes, matchingNodes...), nil
|
return append(originalMatchingNodes, matchingNodes...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printValue(node *yaml.Node, cmd *cobra.Command) error {
|
func printValue(node *yaml.Node, writer io.Writer) error {
|
||||||
if node.Kind == yaml.ScalarNode {
|
if node.Kind == yaml.ScalarNode {
|
||||||
cmd.Print(node.Value)
|
writer.Write([]byte(node.Value))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return printNode(node, cmd.OutOrStdout())
|
return printNode(node, writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printNode(node *yaml.Node, writer io.Writer) error {
|
func printNode(node *yaml.Node, writer io.Writer) error {
|
||||||
bufferedWriter := bufio.NewWriter(writer)
|
|
||||||
defer safelyFlush(bufferedWriter)
|
|
||||||
|
|
||||||
var encoder yqlib.Encoder
|
var encoder yqlib.Encoder
|
||||||
if outputToJSON {
|
if outputToJSON {
|
||||||
encoder = yqlib.NewJsonEncoder(bufferedWriter)
|
encoder = yqlib.NewJsonEncoder(writer)
|
||||||
} else {
|
} else {
|
||||||
encoder = yqlib.NewYamlEncoder(bufferedWriter)
|
encoder = yqlib.NewYamlEncoder(writer)
|
||||||
}
|
}
|
||||||
return encoder.Encode(node)
|
return encoder.Encode(node)
|
||||||
}
|
}
|
||||||
@ -101,11 +97,14 @@ func updateStyleOfNode(node *yaml.Node, style yaml.Style) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error {
|
func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
||||||
|
bufferedWriter := bufio.NewWriter(writer)
|
||||||
|
defer safelyFlush(bufferedWriter)
|
||||||
|
|
||||||
if len(matchingNodes) == 0 {
|
if len(matchingNodes) == 0 {
|
||||||
log.Debug("no matching results, nothing to print")
|
log.Debug("no matching results, nothing to print")
|
||||||
if defaultValue != "" {
|
if defaultValue != "" {
|
||||||
cmd.Print(defaultValue)
|
bufferedWriter.Write([]byte(defaultValue))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -113,9 +112,9 @@ func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error
|
|||||||
for index, mappedDoc := range matchingNodes {
|
for index, mappedDoc := range matchingNodes {
|
||||||
switch printMode {
|
switch printMode {
|
||||||
case "p":
|
case "p":
|
||||||
cmd.Print(lib.PathStackToString(mappedDoc.PathStack))
|
bufferedWriter.Write([]byte(lib.PathStackToString(mappedDoc.PathStack)))
|
||||||
if index < len(matchingNodes)-1 {
|
if index < len(matchingNodes)-1 {
|
||||||
cmd.Print("\n")
|
bufferedWriter.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
case "pv", "vp":
|
case "pv", "vp":
|
||||||
// put it into a node and print that.
|
// put it into a node and print that.
|
||||||
@ -123,17 +122,17 @@ func printResults(matchingNodes []*yqlib.NodeContext, cmd *cobra.Command) error
|
|||||||
parentNode.Content = make([]*yaml.Node, 2)
|
parentNode.Content = make([]*yaml.Node, 2)
|
||||||
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
|
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
|
||||||
parentNode.Content[1] = mappedDoc.Node
|
parentNode.Content[1] = mappedDoc.Node
|
||||||
if err := printValue(&parentNode, cmd); err != nil {
|
if err := printValue(&parentNode, bufferedWriter); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if err := printValue(mappedDoc.Node, cmd); err != nil {
|
if err := printValue(mappedDoc.Node, bufferedWriter); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Printing our Scalars does not print a new line at the end
|
// Printing our Scalars does not print a new line at the end
|
||||||
// we only want to do that if there are more values (so users can easily script extraction of values in the yaml)
|
// we only want to do that if there are more values (so users can easily script extraction of values in the yaml)
|
||||||
if index < len(matchingNodes)-1 && mappedDoc.Node.Kind == yaml.ScalarNode {
|
if index < len(matchingNodes)-1 && mappedDoc.Node.Kind == yaml.ScalarNode {
|
||||||
cmd.Print("\n")
|
bufferedWriter.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user