mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-08 06:45:38 +00:00
Feature: Adds formatting of yaml output
The format produced by `gopkg.in/yaml.v2` produces output while valid is not easy to read especially for arrays. Adds formatter to handle formatting the marshalled yaml content. Applies similar logic used by `encoding/json.Indent`. Resolves: #25
This commit is contained in:
parent
cc7eb84388
commit
57d5afa0ae
@ -166,8 +166,8 @@ gather_facts: false
|
|||||||
hosts: lalaland
|
hosts: lalaland
|
||||||
name: Apply smth
|
name: Apply smth
|
||||||
roles:
|
roles:
|
||||||
- lala
|
- lala
|
||||||
- land
|
- land
|
||||||
serial: 1
|
serial: 1
|
||||||
`
|
`
|
||||||
assertResult(t, expectedOutput, result.Output)
|
assertResult(t, expectedOutput, result.Output)
|
||||||
@ -448,8 +448,8 @@ func TestWriteCmd_Append(t *testing.T) {
|
|||||||
t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
expectedOutput := `b:
|
expectedOutput := `b:
|
||||||
- foo
|
- foo
|
||||||
- 7
|
- 7
|
||||||
`
|
`
|
||||||
assertResult(t, expectedOutput, result.Output)
|
assertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
@ -467,7 +467,7 @@ func TestWriteCmd_AppendEmptyArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
expectedOutput := `a: 2
|
expectedOutput := `a: 2
|
||||||
b:
|
b:
|
||||||
- v
|
- v
|
||||||
`
|
`
|
||||||
assertResult(t, expectedOutput, result.Output)
|
assertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
@ -480,8 +480,8 @@ func TestMergeCmd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
expectedOutput := `a: simple
|
expectedOutput := `a: simple
|
||||||
b:
|
b:
|
||||||
- 1
|
- 1
|
||||||
- 2
|
- 2
|
||||||
c:
|
c:
|
||||||
test: 1
|
test: 1
|
||||||
`
|
`
|
||||||
@ -516,8 +516,8 @@ func TestMergeCmd_Verbose(t *testing.T) {
|
|||||||
}
|
}
|
||||||
expectedOutput := `a: simple
|
expectedOutput := `a: simple
|
||||||
b:
|
b:
|
||||||
- 1
|
- 1
|
||||||
- 2
|
- 2
|
||||||
c:
|
c:
|
||||||
test: 1
|
test: 1
|
||||||
`
|
`
|
||||||
@ -536,8 +536,8 @@ func TestMergeCmd_Inplace(t *testing.T) {
|
|||||||
gotOutput := readTempYamlFile(filename)
|
gotOutput := readTempYamlFile(filename)
|
||||||
expectedOutput := `a: simple
|
expectedOutput := `a: simple
|
||||||
b:
|
b:
|
||||||
- 1
|
- 1
|
||||||
- 2
|
- 2
|
||||||
c:
|
c:
|
||||||
test: 1`
|
test: 1`
|
||||||
assertResult(t, expectedOutput, gotOutput)
|
assertResult(t, expectedOutput, gotOutput)
|
||||||
|
|||||||
@ -249,7 +249,7 @@ b:
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
var expected = `b:
|
var expected = `b:
|
||||||
- c: "4"`
|
- c: "4"`
|
||||||
|
|
||||||
updated := writeMap(data, []string{"b", "0", "c"}, "4")
|
updated := writeMap(data, []string{"b", "0", "c"}, "4")
|
||||||
got, _ := yamlToString(updated)
|
got, _ := yamlToString(updated)
|
||||||
@ -274,8 +274,8 @@ b:
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
var expected = `b:
|
var expected = `b:
|
||||||
- aa
|
- aa
|
||||||
- bb`
|
- bb`
|
||||||
|
|
||||||
updated := writeMap(data, []string{"b", "1"}, "bb")
|
updated := writeMap(data, []string{"b", "1"}, "bb")
|
||||||
got, _ := yamlToString(updated)
|
got, _ := yamlToString(updated)
|
||||||
|
|||||||
36
formatter.go
Normal file
36
formatter.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func format(context interface{}) (string, error) {
|
||||||
|
out, err := yaml.Marshal(context)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error printing yaml: %v", err)
|
||||||
|
}
|
||||||
|
var dst bytes.Buffer
|
||||||
|
// add error checking if indent expanded to ever return an error
|
||||||
|
_ = indent(&dst, out)
|
||||||
|
|
||||||
|
return dst.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(dst io.ByteWriter, src []byte) error {
|
||||||
|
for _, c := range src {
|
||||||
|
switch c {
|
||||||
|
case '-':
|
||||||
|
_ = dst.WriteByte(' ')
|
||||||
|
_ = dst.WriteByte(' ')
|
||||||
|
_ = dst.WriteByte(c)
|
||||||
|
|
||||||
|
default:
|
||||||
|
_ = dst.WriteByte(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
4
yaml.go
4
yaml.go
@ -417,11 +417,11 @@ func toString(context interface{}) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func yamlToString(context interface{}) (string, error) {
|
func yamlToString(context interface{}) (string, error) {
|
||||||
out, err := yaml.Marshal(context)
|
outStr, err := format(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error printing yaml: %v", err)
|
return "", fmt.Errorf("error printing yaml: %v", err)
|
||||||
}
|
}
|
||||||
outStr := string(out)
|
|
||||||
// trim the trailing new line as it's easier for a script to add
|
// trim the trailing new line as it's easier for a script to add
|
||||||
// it in if required than to remove it
|
// it in if required than to remove it
|
||||||
if trimOutput {
|
if trimOutput {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user