mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
fa6fac1a76
* Remove extra backtick * Reword explanation of update * Reword explanation of relative update * Change "remaple" to "remain" * Change "clovver" to "clobber" * Reword explanation of update for comment operators * Reword explanation of relative update for comment operators * Change "array" to "expression" * Change "the golangs" to "Golang's" * Change "golangs" to "Golang's" * Change "can durations" to "can add durations" * Change "array scalars" to "arrays" * Change "beit" to "be it" * Fix typo in `eval` tip * Fix typo in header for `has` operation * Add space before pipe in `line` operator example * Fix typos in explanation of deep array merges * Change "is now used" to "is now used." * Change "object," to "object." * Changes "indexes" to "indices" * Remove extraneous copied text from `..` article * Reword explanation of `...` operator * Change "your are" to "you are" * Add link to `string` operator docs in `select` article * Change "is a" to "parameter specifies" in `string` operators article * Change "new line" to "newline" * Change "golang regex" to "Golang's regex" * Change "golang" to "Golang" * Add period * Remove comma in `subtract` article * Remove duplicate number subtraction example * Remove comma in `traverse` operator article * Clarify use of brackets when `read`ing with special characters
101 lines
1.9 KiB
Go
101 lines
1.9 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type yamlEncoder struct {
|
|
indent int
|
|
colorise bool
|
|
prefs YamlPreferences
|
|
}
|
|
|
|
func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder {
|
|
if indent < 0 {
|
|
indent = 0
|
|
}
|
|
return &yamlEncoder{indent, colorise, prefs}
|
|
}
|
|
|
|
func (ye *yamlEncoder) CanHandleAliases() bool {
|
|
return true
|
|
}
|
|
|
|
func (ye *yamlEncoder) PrintDocumentSeparator(writer io.Writer) error {
|
|
if ye.prefs.PrintDocSeparators {
|
|
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 != "" {
|
|
// the last comment we read didn't have a newline, put one in
|
|
if err := writeString(writer, "\n"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ye *yamlEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
|
|
|
if node.Kind == yaml.ScalarNode && ye.prefs.UnwrapScalar {
|
|
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
|
|
}
|