mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-14 20:45:36 +00:00
Wip split printer
This commit is contained in:
parent
0289128d2a
commit
54b20f68db
@ -44,7 +44,7 @@ type resultsPrinter struct {
|
|||||||
colorsEnabled bool
|
colorsEnabled bool
|
||||||
indent int
|
indent int
|
||||||
printDocSeparators bool
|
printDocSeparators bool
|
||||||
writer io.Writer
|
printerWriter printerWriter
|
||||||
firstTimePrinting bool
|
firstTimePrinting bool
|
||||||
previousDocIndex uint
|
previousDocIndex uint
|
||||||
previousFileIndex int
|
previousFileIndex int
|
||||||
@ -53,9 +53,9 @@ type resultsPrinter struct {
|
|||||||
appendixReader io.Reader
|
appendixReader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPrinter(writer io.Writer, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
|
func NewPrinter(printerWriter printerWriter, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
|
||||||
return &resultsPrinter{
|
return &resultsPrinter{
|
||||||
writer: writer,
|
printerWriter: printerWriter,
|
||||||
outputFormat: outputFormat,
|
outputFormat: outputFormat,
|
||||||
unwrapScalar: unwrapScalar,
|
unwrapScalar: unwrapScalar,
|
||||||
colorsEnabled: colorsEnabled,
|
colorsEnabled: colorsEnabled,
|
||||||
@ -150,6 +150,12 @@ func (p *resultsPrinter) processLeadingContent(mappedDoc *CandidateNode, writer
|
|||||||
|
|
||||||
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
||||||
log.Debug("PrintResults for %v matches", matchingNodes.Len())
|
log.Debug("PrintResults for %v matches", matchingNodes.Len())
|
||||||
|
|
||||||
|
if matchingNodes.Len() == 0 {
|
||||||
|
log.Debug("no matching results, nothing to print")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if p.outputFormat != YamlOutputFormat {
|
if p.outputFormat != YamlOutputFormat {
|
||||||
explodeOp := Operation{OperationType: explodeOpType}
|
explodeOp := Operation{OperationType: explodeOpType}
|
||||||
explodeNode := ExpressionNode{Operation: &explodeOp}
|
explodeNode := ExpressionNode{Operation: &explodeOp}
|
||||||
@ -160,10 +166,6 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
matchingNodes = context.MatchingNodes
|
matchingNodes = context.MatchingNodes
|
||||||
}
|
}
|
||||||
|
|
||||||
if matchingNodes.Len() == 0 {
|
|
||||||
log.Debug("no matching results, nothing to print")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if p.firstTimePrinting {
|
if p.firstTimePrinting {
|
||||||
node := matchingNodes.Front().Value.(*CandidateNode)
|
node := matchingNodes.Front().Value.(*CandidateNode)
|
||||||
p.previousDocIndex = node.Document
|
p.previousDocIndex = node.Document
|
||||||
@ -171,37 +173,49 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
p.firstTimePrinting = false
|
p.firstTimePrinting = false
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferedWriter := bufio.NewWriter(p.writer)
|
index := 0
|
||||||
defer p.safelyFlush(bufferedWriter)
|
|
||||||
|
|
||||||
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
|
|
||||||
mappedDoc := el.Value.(*CandidateNode)
|
mappedDoc := el.Value.(*CandidateNode)
|
||||||
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
|
log.Debug("-- print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v, mappedDoc.Document: %v, printDocSeparators: %v", p.firstTimePrinting, p.previousDocIndex, mappedDoc.Document, p.printDocSeparators)
|
||||||
|
|
||||||
|
writer, errorWriting := p.printerWriter.GetWriter(mappedDoc, index)
|
||||||
|
if errorWriting != nil {
|
||||||
|
return errorWriting
|
||||||
|
}
|
||||||
|
|
||||||
commentStartsWithSeparator := strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$\n$yqDocSeperator$")
|
commentStartsWithSeparator := strings.Contains(mappedDoc.Node.HeadComment, "$yqLeadingContent$\n$yqDocSeperator$")
|
||||||
|
|
||||||
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
|
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
|
||||||
log.Debug("-- writing doc sep")
|
log.Debug("-- writing doc sep")
|
||||||
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
|
if err := p.writeString(writer, "---\n"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.processLeadingContent(mappedDoc, bufferedWriter); err != nil {
|
if err := p.processLeadingContent(mappedDoc, writer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {
|
if err := p.printNode(mappedDoc.Node, writer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.previousDocIndex = mappedDoc.Document
|
p.previousDocIndex = mappedDoc.Document
|
||||||
|
if err := writer.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
index++
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.appendixReader != nil && p.outputFormat == YamlOutputFormat {
|
if p.appendixReader != nil && p.outputFormat == YamlOutputFormat {
|
||||||
|
writer := p.printerWriter.GetWriter(nil, index)
|
||||||
log.Debug("Piping appendix reader...")
|
log.Debug("Piping appendix reader...")
|
||||||
betterReader := bufio.NewReader(p.appendixReader)
|
betterReader := bufio.NewReader(p.appendixReader)
|
||||||
_, err := io.Copy(bufferedWriter, betterReader)
|
_, err := io.Copy(writer, betterReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
52
pkg/yqlib/printer_writer.go
Normal file
52
pkg/yqlib/printer_writer.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type printerWriter interface {
|
||||||
|
GetWriter(node *CandidateNode, index int) (*bufio.Writer, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type singlePrinterWriter struct {
|
||||||
|
bufferedWriter *bufio.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSinglePrinterWriter(writer io.Writer) printerWriter {
|
||||||
|
return &singlePrinterWriter{
|
||||||
|
bufferedWriter: bufio.NewWriter(writer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sp *singlePrinterWriter) GetWriter(node *CandidateNode, i int) (*bufio.Writer, error) {
|
||||||
|
return sp.bufferedWriter, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type multiPrintWriter struct {
|
||||||
|
treeNavigator DataTreeNavigator
|
||||||
|
nameExpression *ExpressionNode
|
||||||
|
extension string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sp *multiPrintWriter) GetWriter(node *CandidateNode, index int) (*bufio.Writer, error) {
|
||||||
|
name := ""
|
||||||
|
|
||||||
|
if sp.nameExpression != nil {
|
||||||
|
context := Context{MatchingNodes: node.AsList()}
|
||||||
|
result, err := sp.treeNavigator.GetMatchingNodes(context, sp.nameExpression)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if result.MatchingNodes.Len() > 0 {
|
||||||
|
name = result.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if name == "" {
|
||||||
|
name = fmt.Sprintf("%v.%v", index, sp.extension)
|
||||||
|
} else {
|
||||||
|
name = fmt.Sprintf("%v.%v", name, sp.extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user