mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Pretty print json
This commit is contained in:
parent
b3598aaa43
commit
166f866f28
@ -627,23 +627,44 @@ func TestReadCmd_Verbose(t *testing.T) {
|
|||||||
test.AssertResult(t, "2", result.Output)
|
test.AssertResult(t, "2", result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestReadCmd_ToJson(t *testing.T) {
|
func TestReadToJsonCmd(t *testing.T) {
|
||||||
// cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
// result := test.RunCmd(cmd, "read -j ../examples/sample.yaml b.c")
|
result := test.RunCmd(cmd, "read -j ../examples/sample.yaml b")
|
||||||
// if result.Error != nil {
|
if result.Error != nil {
|
||||||
// t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
// }
|
}
|
||||||
// test.AssertResult(t, "2\n", result.Output)
|
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) {
|
func TestReadToJsonPrettyCmd(t *testing.T) {
|
||||||
// cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
// result := test.RunCmd(cmd, "read --tojson ../examples/sample.yaml b.c")
|
result := test.RunCmd(cmd, "read -j -P ../examples/sample.yaml b")
|
||||||
// if result.Error != nil {
|
if result.Error != nil {
|
||||||
// t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
// }
|
}
|
||||||
// test.AssertResult(t, "2\n", result.Output)
|
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) {
|
func TestReadBadDataCmd(t *testing.T) {
|
||||||
content := `[!Whatever]`
|
content := `[!Whatever]`
|
||||||
|
@ -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(&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().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")
|
cmdCompare.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
|
||||||
return cmdCompare
|
return cmdCompare
|
||||||
}
|
}
|
||||||
@ -60,11 +60,6 @@ func compareDocuments(cmd *cobra.Command, args []string) error {
|
|||||||
return errorDoingThings
|
return errorDoingThings
|
||||||
}
|
}
|
||||||
|
|
||||||
if prettyPrint {
|
|
||||||
setStyle(matchingNodesA, 0)
|
|
||||||
setStyle(matchingNodesB, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataBufferA bytes.Buffer
|
var dataBufferA bytes.Buffer
|
||||||
var dataBufferB bytes.Buffer
|
var dataBufferB bytes.Buffer
|
||||||
errorDoingThings = printResults(matchingNodesA, bufio.NewWriter(&dataBufferA))
|
errorDoingThings = printResults(matchingNodesA, bufio.NewWriter(&dataBufferA))
|
||||||
|
@ -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(&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().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")
|
cmdRead.PersistentFlags().StringVarP(&defaultValue, "defaultValue", "D", "", "default value printed when there are no results")
|
||||||
return cmdRead
|
return cmdRead
|
||||||
}
|
}
|
||||||
@ -50,9 +50,5 @@ func readProperty(cmd *cobra.Command, args []string) error {
|
|||||||
return errorReadingStream
|
return errorReadingStream
|
||||||
}
|
}
|
||||||
|
|
||||||
if prettyPrint {
|
|
||||||
setStyle(matchingNodes, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return printResults(matchingNodes, cmd.OutOrStdout())
|
return printResults(matchingNodes, cmd.OutOrStdout())
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func printValue(node *yaml.Node, writer io.Writer) error {
|
|||||||
func printNode(node *yaml.Node, writer io.Writer) error {
|
func printNode(node *yaml.Node, writer io.Writer) error {
|
||||||
var encoder yqlib.Encoder
|
var encoder yqlib.Encoder
|
||||||
if outputToJSON {
|
if outputToJSON {
|
||||||
encoder = yqlib.NewJsonEncoder(writer)
|
encoder = yqlib.NewJsonEncoder(writer, prettyPrint)
|
||||||
} else {
|
} else {
|
||||||
encoder = yqlib.NewYamlEncoder(writer)
|
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 {
|
func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
|
||||||
|
if prettyPrint {
|
||||||
|
setStyle(matchingNodes, 0)
|
||||||
|
}
|
||||||
|
|
||||||
bufferedWriter := bufio.NewWriter(writer)
|
bufferedWriter := bufio.NewWriter(writer)
|
||||||
defer safelyFlush(bufferedWriter)
|
defer safelyFlush(bufferedWriter)
|
||||||
|
|
||||||
@ -281,7 +285,7 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
|
|||||||
|
|
||||||
var encoder yqlib.Encoder
|
var encoder yqlib.Encoder
|
||||||
if outputToJSON {
|
if outputToJSON {
|
||||||
encoder = yqlib.NewJsonEncoder(bufferedWriter)
|
encoder = yqlib.NewJsonEncoder(bufferedWriter, prettyPrint)
|
||||||
} else {
|
} else {
|
||||||
encoder = yqlib.NewYamlEncoder(bufferedWriter)
|
encoder = yqlib.NewYamlEncoder(bufferedWriter)
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,11 @@ type jsonEncoder struct {
|
|||||||
encoder *json.Encoder
|
encoder *json.Encoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJsonEncoder(destination io.Writer) Encoder {
|
func NewJsonEncoder(destination io.Writer, prettyPrint bool) Encoder {
|
||||||
var encoder = json.NewEncoder(destination)
|
var encoder = json.NewEncoder(destination)
|
||||||
|
if prettyPrint {
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
}
|
||||||
return &jsonEncoder{encoder}
|
return &jsonEncoder{encoder}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user