2022-01-15 00:57:59 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type yamlEncoder struct {
|
2022-10-28 03:16:46 +00:00
|
|
|
indent int
|
|
|
|
colorise bool
|
|
|
|
prefs YamlPreferences
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder {
|
2022-01-15 00:57:59 +00:00
|
|
|
if indent < 0 {
|
|
|
|
indent = 0
|
|
|
|
}
|
2022-10-28 03:16:46 +00:00
|
|
|
return &yamlEncoder{indent, colorise, prefs}
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ye *yamlEncoder) CanHandleAliases() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ye *yamlEncoder) PrintDocumentSeparator(writer io.Writer) error {
|
2022-10-28 03:16:46 +00:00
|
|
|
if ye.prefs.PrintDocSeparators {
|
2022-01-15 00:57:59 +00:00
|
|
|
log.Debug("-- writing doc sep")
|
|
|
|
if err := writeString(writer, "---\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ye *yamlEncoder) PrintLeadingContent(writer io.Writer, content string) error {
|
|
|
|
// log.Debug("headcommentwas [%v]", content)
|
|
|
|
reader := bufio.NewReader(strings.NewReader(content))
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
readline, errReading := reader.ReadString('\n')
|
|
|
|
if errReading != nil && !errors.Is(errReading, io.EOF) {
|
|
|
|
return errReading
|
|
|
|
}
|
|
|
|
if strings.Contains(readline, "$yqDocSeperator$") {
|
|
|
|
|
|
|
|
if err := ye.PrintDocumentSeparator(writer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if err := writeString(writer, readline); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(errReading, io.EOF) {
|
|
|
|
if readline != "" {
|
2023-03-16 02:39:36 +00:00
|
|
|
// the last comment we read didn't have a newline, put one in
|
2022-01-15 00:57:59 +00:00
|
|
|
if err := writeString(writer, "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-08 09:56:35 +00:00
|
|
|
func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2023-04-08 09:56:35 +00:00
|
|
|
if node.Kind == ScalarNode && ye.prefs.UnwrapScalar {
|
2022-01-15 00:57:59 +00:00
|
|
|
return writeString(writer, node.Value+"\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
destination := writer
|
|
|
|
tempBuffer := bytes.NewBuffer(nil)
|
|
|
|
if ye.colorise {
|
|
|
|
destination = tempBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
var encoder = yaml.NewEncoder(destination)
|
|
|
|
|
|
|
|
encoder.SetIndent(ye.indent)
|
|
|
|
|
|
|
|
if err := encoder.Encode(node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ye.colorise {
|
|
|
|
return colorizeAndPrint(tempBuffer.Bytes(), writer)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|