From 9e476852711bca2cdb3d2e3b6ee7ca9cb6a8e91e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 3 Feb 2020 13:59:16 +1100 Subject: [PATCH] Compare first cut --- cmd/compare.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + cmd/utils.go | 10 +++---- go.mod | 1 + go.sum | 2 ++ 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 cmd/compare.go diff --git a/cmd/compare.go b/cmd/compare.go new file mode 100644 index 00000000..211c668e --- /dev/null +++ b/cmd/compare.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "bufio" + "bytes" + + "github.com/kylelemons/godebug/diff" + errors "github.com/pkg/errors" + "github.com/spf13/cobra" + yaml "gopkg.in/yaml.v3" +) + +func createCompareCmd() *cobra.Command { + var cmdCompare = &cobra.Command{ + Use: "compare [yaml_file_a] [yaml_file_b]", + Aliases: []string{"x"}, + Short: "yq x data1.yml data2.yml", + Example: ` +yq x - data2.yml # reads from stdin +`, + Long: "Compares two yaml files, prints the difference", + RunE: compareDocuments, + } + cmdCompare.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based)") + cmdCompare.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print (does not have an affect with json output)") + return cmdCompare +} + +func compareDocuments(cmd *cobra.Command, args []string) error { + if len(args) != 2 { + return errors.New("Must provide at 2 yaml files") + } + if docIndex == "*" { + return errors.New("Document splat for compare not yet supported") + } + + var _, docIndexInt, errorParsingDocIndex = parseDocumentIndex() + if errorParsingDocIndex != nil { + return errorParsingDocIndex + } + + var dataBucketA yaml.Node + var dataBucketB yaml.Node + var errorReadingStream error + errorReadingStream = readData(args[0], docIndexInt, &dataBucketA) + if errorReadingStream != nil { + return errorReadingStream + } + + errorReadingStream = readData(args[1], docIndexInt, &dataBucketB) + if errorReadingStream != nil { + return errorReadingStream + } + + if prettyPrint { + updateStyleOfNode(&dataBucketA, 0) + updateStyleOfNode(&dataBucketB, 0) + } + + if errorReadingStream != nil { + return errorReadingStream + } + + var dataBufferA bytes.Buffer + printNode(&dataBucketA, bufio.NewWriter(&dataBufferA)) + + var dataBufferB bytes.Buffer + printNode(&dataBucketB, bufio.NewWriter(&dataBufferB)) + + cmd.Print(diff.Diff(dataBufferA.String(), dataBufferB.String())) + return nil +} diff --git a/cmd/root.go b/cmd/root.go index f8eb8f63..e435b6af 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,6 +44,7 @@ func New() *cobra.Command { rootCmd.AddCommand( createReadCmd(), + createCompareCmd(), createValidateCmd(), createWriteCmd(), createPrefixCmd(), diff --git a/cmd/utils.go b/cmd/utils.go index 878f0dee..ed6cf532 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -71,8 +71,11 @@ func printValue(node *yaml.Node, cmd *cobra.Command) error { cmd.Print(node.Value) return nil } + return printNode(node, cmd.OutOrStdout()) +} - bufferedWriter := bufio.NewWriter(cmd.OutOrStdout()) +func printNode(node *yaml.Node, writer io.Writer) error { + bufferedWriter := bufio.NewWriter(writer) defer safelyFlush(bufferedWriter) var encoder yqlib.Encoder @@ -81,10 +84,7 @@ func printValue(node *yaml.Node, cmd *cobra.Command) error { } else { encoder = yqlib.NewYamlEncoder(bufferedWriter) } - if err := encoder.Encode(node); err != nil { - return err - } - return nil + return encoder.Encode(node) } func setStyle(matchingNodes []*yqlib.NodeContext, style yaml.Style) { diff --git a/go.mod b/go.mod index b6863a43..ef0a88db 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/mikefarah/yq/v3 require ( + github.com/kylelemons/godebug v1.1.0 github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.5 golang.org/x/tools v0.0.0-20191213221258-04c2e8eff935 // indirect diff --git a/go.sum b/go.sum index 2f045e24..9235f824 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mikefarah/yaml v2.1.0+incompatible h1:nu2cqmzk4WlWJNgnevY88faMcdrDzYGcsUjYFxEpB7Y= github.com/mikefarah/yaml/v2 v2.4.0 h1:eYqfooY0BnvKTJxr7+ABJs13n3dg9n347GScDaU2Lww=