diff --git a/cmd/commands_test.go b/cmd/commands_test.go index e21d8c22..1fe36924 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -439,6 +439,27 @@ b: test.AssertResult(t, expectedOutput, result.Output) } +func TestReadPrettyPrintWithIndentCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -P -I4 ../examples/sample.json") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `a: Easy! as one two three +b: + c: 2 + d: + - 3 + - 4 + e: + - name: fred + value: 3 + - name: sam + value: 4 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadCmd_ArrayYaml_NoPath(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read ../examples/array.yaml") @@ -645,22 +666,50 @@ func TestReadToJsonPrettyCmd(t *testing.T) { t.Error(result.Error) } expectedOutput := `{ - "c": 2, - "d": [ - 3, - 4, - 5 - ], - "e": [ - { - "name": "fred", - "value": 3 - }, - { - "name": "sam", - "value": 4 - } - ] + "c": 2, + "d": [ + 3, + 4, + 5 + ], + "e": [ + { + "name": "fred", + "value": 3 + }, + { + "name": "sam", + "value": 4 + } + ] +} +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestReadToJsonPrettyIndentCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -j -I4 -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) diff --git a/cmd/constant.go b/cmd/constant.go index 49043371..4c1c340e 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -12,6 +12,7 @@ var writeScript = "" var outputToJSON = false var prettyPrint = false var defaultValue = "" +var indent = 2 var overwriteFlag = false var autoCreateFlag = true var allowEmptyFlag = false diff --git a/cmd/root.go b/cmd/root.go index 6c8c91f7..844fde10 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,8 +39,9 @@ func New() *cobra.Command { } rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") - rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") + rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json. By default it prints a json document in one line, use the prettyPrint flag to print a formatted doc.") rootCmd.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print") + rootCmd.PersistentFlags().IntVarP(&indent, "indent", "I", 2, "sets indent level for output") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit") rootCmd.AddCommand( diff --git a/cmd/utils.go b/cmd/utils.go index a9c5ddea..d4e473c4 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -76,9 +76,9 @@ 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, prettyPrint) + encoder = yqlib.NewJsonEncoder(writer, prettyPrint, indent) } else { - encoder = yqlib.NewYamlEncoder(writer) + encoder = yqlib.NewYamlEncoder(writer, indent) } return encoder.Encode(node) } @@ -289,9 +289,9 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) var encoder yqlib.Encoder if outputToJSON { - encoder = yqlib.NewJsonEncoder(bufferedWriter, prettyPrint) + encoder = yqlib.NewJsonEncoder(bufferedWriter, prettyPrint, indent) } else { - encoder = yqlib.NewYamlEncoder(bufferedWriter) + encoder = yqlib.NewYamlEncoder(bufferedWriter, indent) } return readStream(inputFile, mapYamlDecoder(updateData, encoder)) } diff --git a/pkg/yqlib/encoder.go b/pkg/yqlib/encoder.go index afdadb24..5a4be048 100644 --- a/pkg/yqlib/encoder.go +++ b/pkg/yqlib/encoder.go @@ -15,9 +15,12 @@ type yamlEncoder struct { encoder *yaml.Encoder } -func NewYamlEncoder(destination io.Writer) Encoder { +func NewYamlEncoder(destination io.Writer, indent int) Encoder { var encoder = yaml.NewEncoder(destination) - encoder.SetIndent(2) + if indent < 0 { + indent = 0 + } + encoder.SetIndent(indent) return &yamlEncoder{encoder} } @@ -29,10 +32,15 @@ type jsonEncoder struct { encoder *json.Encoder } -func NewJsonEncoder(destination io.Writer, prettyPrint bool) Encoder { +func NewJsonEncoder(destination io.Writer, prettyPrint bool, indent int) Encoder { var encoder = json.NewEncoder(destination) + var indentString = "" + + for index := 0; index < indent; index++ { + indentString = indentString + " " + } if prettyPrint { - encoder.SetIndent("", " ") + encoder.SetIndent("", indentString) } return &jsonEncoder{encoder} }