mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Compare first cut
This commit is contained in:
parent
699fce9da4
commit
9e47685271
72
cmd/compare.go
Normal file
72
cmd/compare.go
Normal file
@ -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
|
||||||
|
}
|
@ -44,6 +44,7 @@ func New() *cobra.Command {
|
|||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
createReadCmd(),
|
createReadCmd(),
|
||||||
|
createCompareCmd(),
|
||||||
createValidateCmd(),
|
createValidateCmd(),
|
||||||
createWriteCmd(),
|
createWriteCmd(),
|
||||||
createPrefixCmd(),
|
createPrefixCmd(),
|
||||||
|
10
cmd/utils.go
10
cmd/utils.go
@ -71,8 +71,11 @@ func printValue(node *yaml.Node, cmd *cobra.Command) error {
|
|||||||
cmd.Print(node.Value)
|
cmd.Print(node.Value)
|
||||||
return nil
|
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)
|
defer safelyFlush(bufferedWriter)
|
||||||
|
|
||||||
var encoder yqlib.Encoder
|
var encoder yqlib.Encoder
|
||||||
@ -81,10 +84,7 @@ func printValue(node *yaml.Node, cmd *cobra.Command) error {
|
|||||||
} else {
|
} else {
|
||||||
encoder = yqlib.NewYamlEncoder(bufferedWriter)
|
encoder = yqlib.NewYamlEncoder(bufferedWriter)
|
||||||
}
|
}
|
||||||
if err := encoder.Encode(node); err != nil {
|
return encoder.Encode(node)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setStyle(matchingNodes []*yqlib.NodeContext, style yaml.Style) {
|
func setStyle(matchingNodes []*yqlib.NodeContext, style yaml.Style) {
|
||||||
|
1
go.mod
1
go.mod
@ -1,6 +1,7 @@
|
|||||||
module github.com/mikefarah/yq/v3
|
module github.com/mikefarah/yq/v3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/kylelemons/godebug v1.1.0
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
golang.org/x/tools v0.0.0-20191213221258-04c2e8eff935 // indirect
|
golang.org/x/tools v0.0.0-20191213221258-04c2e8eff935 // indirect
|
||||||
|
2
go.sum
2
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/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 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
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/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.1.0+incompatible h1:nu2cqmzk4WlWJNgnevY88faMcdrDzYGcsUjYFxEpB7Y=
|
||||||
github.com/mikefarah/yaml/v2 v2.4.0 h1:eYqfooY0BnvKTJxr7+ABJs13n3dg9n347GScDaU2Lww=
|
github.com/mikefarah/yaml/v2 v2.4.0 h1:eYqfooY0BnvKTJxr7+ABJs13n3dg9n347GScDaU2Lww=
|
||||||
|
Loading…
Reference in New Issue
Block a user