mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 22:35:37 +00:00
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
37 lines
611 B
Go
37 lines
611 B
Go
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
|
|
}
|