mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 22:35:37 +00:00
Merge 57d5afa0ae into cc7eb84388
This commit is contained in:
commit
4cb2c74559
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) {
|
||||
out, err := yaml.Marshal(context)
|
||||
outStr, err := format(context)
|
||||
if err != nil {
|
||||
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
|
||||
// it in if required than to remove it
|
||||
if trimOutput {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user