Pretty print json

This commit is contained in:
Mike Farah 2020-02-03 16:31:03 +11:00
parent b3598aaa43
commit 166f866f28
5 changed files with 49 additions and 30 deletions

View File

@ -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]`

View File

@ -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))

View File

@ -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())
}

View File

@ -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)
}

View File

@ -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}
}