From 166f866f28849a1468cb890af6f062c4e9068693 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 3 Feb 2020 16:31:03 +1100 Subject: [PATCH] Pretty print json --- cmd/commands_test.go | 53 +++++++++++++++++++++++++++++++------------- cmd/compare.go | 7 +----- cmd/read.go | 6 +---- cmd/utils.go | 8 +++++-- pkg/yqlib/encoder.go | 5 ++++- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 4e8ba72d..97a770dd 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -627,23 +627,44 @@ func TestReadCmd_Verbose(t *testing.T) { test.AssertResult(t, "2", result.Output) } -// func TestReadCmd_ToJson(t *testing.T) { -// cmd := getRootCommand() -// result := test.RunCmd(cmd, "read -j ../examples/sample.yaml b.c") -// if result.Error != nil { -// t.Error(result.Error) -// } -// test.AssertResult(t, "2\n", result.Output) -// } +func TestReadToJsonCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -j ../examples/sample.yaml b") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `{"c":2,"d":[3,4,5],"e":[{"name":"fred","value":3},{"name":"sam","value":4}]} +` + test.AssertResult(t, expectedOutput, result.Output) +} -// func TestReadCmd_ToJsonLong(t *testing.T) { -// cmd := getRootCommand() -// result := test.RunCmd(cmd, "read --tojson ../examples/sample.yaml b.c") -// if result.Error != nil { -// t.Error(result.Error) -// } -// test.AssertResult(t, "2\n", result.Output) -// } +func TestReadToJsonPrettyCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -j -P ../examples/sample.yaml b") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `{ + "c": 2, + "d": [ + 3, + 4, + 5 + ], + "e": [ + { + "name": "fred", + "value": 3 + }, + { + "name": "sam", + "value": 4 + } + ] +} +` + test.AssertResult(t, expectedOutput, result.Output) +} func TestReadBadDataCmd(t *testing.T) { content := `[!Whatever]` diff --git a/cmd/compare.go b/cmd/compare.go index 310bb0b1..ab735e45 100644 --- a/cmd/compare.go +++ b/cmd/compare.go @@ -26,7 +26,7 @@ yq x -d1 dataA.yaml dataB.yaml 'a.b.c' } 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(ish) print") cmdCompare.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results") return cmdCompare } @@ -60,11 +60,6 @@ func compareDocuments(cmd *cobra.Command, args []string) error { return errorDoingThings } - if prettyPrint { - setStyle(matchingNodesA, 0) - setStyle(matchingNodesB, 0) - } - var dataBufferA bytes.Buffer var dataBufferB bytes.Buffer errorDoingThings = printResults(matchingNodesA, bufio.NewWriter(&dataBufferA)) diff --git a/cmd/read.go b/cmd/read.go index 1a60c9db..a4b26cc6 100644 --- a/cmd/read.go +++ b/cmd/read.go @@ -25,7 +25,7 @@ yq r -- things.yaml '--key-starting-with-dashes.blah' } cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)") cmdRead.PersistentFlags().StringVarP(&printMode, "printMode", "p", "v", "print mode (v (values, default), p (paths), pv (path and value pairs)") - cmdRead.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print (does not have an affect with json output)") + cmdRead.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty(ish) print") cmdRead.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results") return cmdRead } @@ -50,9 +50,5 @@ func readProperty(cmd *cobra.Command, args []string) error { return errorReadingStream } - if prettyPrint { - setStyle(matchingNodes, 0) - } - return printResults(matchingNodes, cmd.OutOrStdout()) } diff --git a/cmd/utils.go b/cmd/utils.go index 7cd5f662..428228d6 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -76,7 +76,7 @@ func printValue(node *yaml.Node, writer io.Writer) error { func printNode(node *yaml.Node, writer io.Writer) error { var encoder yqlib.Encoder if outputToJSON { - encoder = yqlib.NewJsonEncoder(writer) + encoder = yqlib.NewJsonEncoder(writer, prettyPrint) } else { encoder = yqlib.NewYamlEncoder(writer) } @@ -103,6 +103,10 @@ func writeString(writer io.Writer, txt string) error { } func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { + if prettyPrint { + setStyle(matchingNodes, 0) + } + bufferedWriter := bufio.NewWriter(writer) defer safelyFlush(bufferedWriter) @@ -281,7 +285,7 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) var encoder yqlib.Encoder if outputToJSON { - encoder = yqlib.NewJsonEncoder(bufferedWriter) + encoder = yqlib.NewJsonEncoder(bufferedWriter, prettyPrint) } else { encoder = yqlib.NewYamlEncoder(bufferedWriter) } diff --git a/pkg/yqlib/encoder.go b/pkg/yqlib/encoder.go index affd213e..afdadb24 100644 --- a/pkg/yqlib/encoder.go +++ b/pkg/yqlib/encoder.go @@ -29,8 +29,11 @@ type jsonEncoder struct { encoder *json.Encoder } -func NewJsonEncoder(destination io.Writer) Encoder { +func NewJsonEncoder(destination io.Writer, prettyPrint bool) Encoder { var encoder = json.NewEncoder(destination) + if prettyPrint { + encoder.SetIndent("", " ") + } return &jsonEncoder{encoder} }