This commit is contained in:
Kenny Jones 2017-09-26 11:15:06 +00:00 committed by GitHub
commit 4cb2c74559
5 changed files with 57 additions and 21 deletions

View File

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

View File

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

View File

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