2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"container/list"
|
|
|
|
"io"
|
|
|
|
|
2020-11-29 09:25:47 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-11-03 23:48:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Printer interface {
|
2020-11-13 02:19:54 +00:00
|
|
|
PrintResults(matchingNodes *list.List) error
|
2020-11-30 05:35:21 +00:00
|
|
|
PrintedAnything() bool
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type resultsPrinter struct {
|
|
|
|
outputToJSON bool
|
|
|
|
unwrapScalar bool
|
|
|
|
colorsEnabled bool
|
|
|
|
indent int
|
|
|
|
printDocSeparators bool
|
2020-11-13 02:19:54 +00:00
|
|
|
writer io.Writer
|
|
|
|
firstTimePrinting bool
|
2020-11-16 01:09:57 +00:00
|
|
|
previousDocIndex uint
|
2020-12-15 03:33:50 +00:00
|
|
|
previousFileIndex int
|
2020-11-30 05:35:21 +00:00
|
|
|
printedMatches bool
|
2021-01-12 23:04:52 +00:00
|
|
|
treeNavigator DataTreeNavigator
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
|
|
|
|
return &resultsPrinter{
|
|
|
|
writer: writer,
|
|
|
|
outputToJSON: outputToJSON,
|
|
|
|
unwrapScalar: unwrapScalar,
|
|
|
|
colorsEnabled: colorsEnabled,
|
|
|
|
indent: indent,
|
|
|
|
printDocSeparators: printDocSeparators,
|
|
|
|
firstTimePrinting: true,
|
2021-01-12 23:04:52 +00:00
|
|
|
treeNavigator: NewDataTreeNavigator(),
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 05:35:21 +00:00
|
|
|
func (p *resultsPrinter) PrintedAnything() bool {
|
|
|
|
return p.printedMatches
|
|
|
|
}
|
|
|
|
|
2020-11-03 23:48:43 +00:00
|
|
|
func (p *resultsPrinter) printNode(node *yaml.Node, writer io.Writer) error {
|
2020-11-30 05:35:21 +00:00
|
|
|
p.printedMatches = p.printedMatches || (node.Tag != "!!null" &&
|
|
|
|
(node.Tag != "!!bool" || node.Value != "false"))
|
|
|
|
|
2020-11-03 23:48:43 +00:00
|
|
|
var encoder Encoder
|
|
|
|
if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON {
|
|
|
|
return p.writeString(writer, node.Value+"\n")
|
|
|
|
}
|
|
|
|
if p.outputToJSON {
|
|
|
|
encoder = NewJsonEncoder(writer, p.indent)
|
|
|
|
} else {
|
|
|
|
encoder = NewYamlEncoder(writer, p.indent, p.colorsEnabled)
|
|
|
|
}
|
|
|
|
return encoder.Encode(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *resultsPrinter) writeString(writer io.Writer, txt string) error {
|
|
|
|
_, errorWriting := writer.Write([]byte(txt))
|
|
|
|
return errorWriting
|
|
|
|
}
|
|
|
|
|
2020-11-29 09:25:47 +00:00
|
|
|
func (p *resultsPrinter) safelyFlush(writer *bufio.Writer) {
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
log.Error("Error flushing writer!")
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
2020-11-16 01:09:57 +00:00
|
|
|
log.Debug("PrintResults for %v matches", matchingNodes.Len())
|
2020-11-06 03:37:01 +00:00
|
|
|
var err error
|
|
|
|
if p.outputToJSON {
|
2021-01-11 06:13:48 +00:00
|
|
|
explodeOp := Operation{OperationType: explodeOpType}
|
2020-11-06 03:37:01 +00:00
|
|
|
explodeNode := PathTreeNode{Operation: &explodeOp}
|
2021-01-12 23:04:52 +00:00
|
|
|
matchingNodes, err = p.treeNavigator.GetMatchingNodes(matchingNodes, &explodeNode)
|
2020-11-06 03:37:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
bufferedWriter := bufio.NewWriter(p.writer)
|
2020-11-29 09:25:47 +00:00
|
|
|
defer p.safelyFlush(bufferedWriter)
|
2020-11-03 23:48:43 +00:00
|
|
|
|
|
|
|
if matchingNodes.Len() == 0 {
|
|
|
|
log.Debug("no matching results, nothing to print")
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 01:09:57 +00:00
|
|
|
if p.firstTimePrinting {
|
2020-12-15 03:33:50 +00:00
|
|
|
node := matchingNodes.Front().Value.(*CandidateNode)
|
|
|
|
p.previousDocIndex = node.Document
|
|
|
|
p.previousFileIndex = node.FileIndex
|
2020-11-16 01:09:57 +00:00
|
|
|
p.firstTimePrinting = false
|
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
|
|
|
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
mappedDoc := el.Value.(*CandidateNode)
|
2020-11-16 01:09:57 +00:00
|
|
|
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
|
2020-12-15 03:33:50 +00:00
|
|
|
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators {
|
2020-11-16 01:09:57 +00:00
|
|
|
log.Debug("-- writing doc sep")
|
2020-11-13 03:07:11 +00:00
|
|
|
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-16 01:09:57 +00:00
|
|
|
p.previousDocIndex = mappedDoc.Document
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|