can set indent levels

This commit is contained in:
Mike Farah 2020-02-03 16:52:12 +11:00
parent 70b88fa778
commit 6840ea8c78
5 changed files with 84 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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