yq/pkg/yqlib/printer.go

195 lines
5.4 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
import (
"bufio"
"container/list"
2021-07-25 08:08:33 +00:00
"fmt"
2020-11-03 23:48:43 +00:00
"io"
2021-11-13 23:31:37 +00:00
"regexp"
2020-11-03 23:48:43 +00:00
2020-11-29 09:25:47 +00:00
yaml "gopkg.in/yaml.v3"
2020-11-03 23:48:43 +00:00
)
type Printer interface {
PrintResults(matchingNodes *list.List) error
2020-11-30 05:35:21 +00:00
PrintedAnything() bool
2021-07-18 02:28:46 +00:00
//e.g. when given a front-matter doc, like jekyll
SetAppendix(reader io.Reader)
2020-11-03 23:48:43 +00:00
}
2021-07-25 08:08:33 +00:00
type PrinterOutputFormat uint32
const (
YamlOutputFormat = 1 << iota
JsonOutputFormat
PropsOutputFormat
2021-12-01 01:08:47 +00:00
CsvOutputFormat
TsvOutputFormat
2021-12-21 05:19:27 +00:00
XmlOutputFormat
2021-07-25 08:08:33 +00:00
)
func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
switch format {
2021-08-20 05:46:33 +00:00
case "yaml", "y":
2021-07-25 08:08:33 +00:00
return YamlOutputFormat, nil
2021-08-20 05:46:33 +00:00
case "json", "j":
2021-07-25 08:08:33 +00:00
return JsonOutputFormat, nil
2021-08-20 05:46:33 +00:00
case "props", "p":
2021-07-25 08:08:33 +00:00
return PropsOutputFormat, nil
2021-12-01 01:08:47 +00:00
case "csv", "c":
return CsvOutputFormat, nil
case "tsv", "t":
return TsvOutputFormat, nil
2021-12-21 05:19:27 +00:00
case "xml", "x":
return XmlOutputFormat, nil
2021-07-25 08:08:33 +00:00
default:
2021-12-21 05:52:54 +00:00
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format)
2021-07-25 08:08:33 +00:00
}
}
2020-11-03 23:48:43 +00:00
type resultsPrinter struct {
2021-07-25 08:08:33 +00:00
outputFormat PrinterOutputFormat
2020-11-03 23:48:43 +00:00
unwrapScalar bool
colorsEnabled bool
indent int
printDocSeparators bool
2021-10-29 03:14:39 +00:00
printerWriter PrinterWriter
2020-11-13 02:19:54 +00:00
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
2021-07-18 02:28:46 +00:00
appendixReader io.Reader
2020-11-03 23:48:43 +00:00
}
2021-10-29 03:14:39 +00:00
func NewPrinterWithSingleWriter(writer io.Writer, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
return NewPrinter(NewSinglePrinterWriter(writer), outputFormat, unwrapScalar, colorsEnabled, indent, printDocSeparators)
}
func NewPrinter(printerWriter PrinterWriter, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
2020-11-13 02:19:54 +00:00
return &resultsPrinter{
2021-10-29 03:14:39 +00:00
printerWriter: printerWriter,
2021-07-25 08:08:33 +00:00
outputFormat: outputFormat,
2020-11-13 02:19:54 +00:00
unwrapScalar: unwrapScalar,
colorsEnabled: colorsEnabled,
indent: indent,
2021-07-25 08:08:33 +00:00
printDocSeparators: outputFormat == YamlOutputFormat && printDocSeparators,
2020-11-13 02:19:54 +00:00
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
}
2021-07-18 02:28:46 +00:00
func (p *resultsPrinter) SetAppendix(reader io.Reader) {
p.appendixReader = reader
}
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
2021-07-25 08:08:33 +00:00
if node.Kind == yaml.ScalarNode && p.unwrapScalar && p.outputFormat == YamlOutputFormat {
2021-11-12 04:02:28 +00:00
return writeString(writer, node.Value+"\n")
2020-11-03 23:48:43 +00:00
}
2021-07-25 08:08:33 +00:00
2021-12-01 01:08:47 +00:00
switch p.outputFormat {
case JsonOutputFormat:
2020-11-03 23:48:43 +00:00
encoder = NewJsonEncoder(writer, p.indent)
2021-12-01 01:08:47 +00:00
case PropsOutputFormat:
2021-07-25 08:08:33 +00:00
encoder = NewPropertiesEncoder(writer)
2021-12-01 01:08:47 +00:00
case CsvOutputFormat:
encoder = NewCsvEncoder(writer, ',')
case TsvOutputFormat:
encoder = NewCsvEncoder(writer, '\t')
case YamlOutputFormat:
2020-11-03 23:48:43 +00:00
encoder = NewYamlEncoder(writer, p.indent, p.colorsEnabled)
2021-12-21 05:19:27 +00:00
case XmlOutputFormat:
2021-12-21 05:52:54 +00:00
encoder = NewXmlEncoder(writer, p.indent, XmlPreferences.AttributePrefix, XmlPreferences.ContentName)
2020-11-03 23:48:43 +00:00
}
2021-12-01 01:08:47 +00:00
2020-11-03 23:48:43 +00:00
return encoder.Encode(node)
}
func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
2020-11-16 01:09:57 +00:00
log.Debug("PrintResults for %v matches", matchingNodes.Len())
2021-10-29 03:14:39 +00:00
if matchingNodes.Len() == 0 {
log.Debug("no matching results, nothing to print")
return nil
}
2021-07-25 08:08:33 +00:00
if p.outputFormat != YamlOutputFormat {
explodeOp := Operation{OperationType: explodeOpType}
2021-01-12 23:18:53 +00:00
explodeNode := ExpressionNode{Operation: &explodeOp}
context, err := p.treeNavigator.GetMatchingNodes(Context{MatchingNodes: matchingNodes}, &explodeNode)
2020-11-06 03:37:01 +00:00
if err != nil {
return err
}
matchingNodes = context.MatchingNodes
2020-11-06 03:37:01 +00:00
}
2020-11-03 23:48:43 +00:00
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() {
2021-10-29 03:14:39 +00:00
2020-11-03 23:48:43 +00:00
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)
2021-10-29 03:14:39 +00:00
writer, errorWriting := p.printerWriter.GetWriter(mappedDoc)
if errorWriting != nil {
return errorWriting
}
2021-11-13 23:31:37 +00:00
commentsStartWithSepExp := regexp.MustCompile(`^\$yqDocSeperator\$`)
commentStartsWithSeparator := commentsStartWithSepExp.MatchString(mappedDoc.LeadingContent)
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
2020-11-16 01:09:57 +00:00
log.Debug("-- writing doc sep")
2021-11-12 04:02:28 +00:00
if err := writeString(writer, "---\n"); err != nil {
2020-11-13 03:07:11 +00:00
return err
}
2020-11-03 23:48:43 +00:00
}
2021-11-12 04:02:28 +00:00
if err := processLeadingContent(mappedDoc, writer, p.printDocSeparators, p.outputFormat); err != nil {
2021-10-29 03:14:39 +00:00
return err
}
2021-07-25 08:08:33 +00:00
2021-10-29 03:14:39 +00:00
if err := p.printNode(mappedDoc.Node, writer); err != nil {
2020-11-03 23:48:43 +00:00
return err
}
2020-11-16 01:09:57 +00:00
p.previousDocIndex = mappedDoc.Document
2021-10-29 03:14:39 +00:00
if err := writer.Flush(); err != nil {
return err
}
2020-11-03 23:48:43 +00:00
}
2021-07-25 08:08:33 +00:00
if p.appendixReader != nil && p.outputFormat == YamlOutputFormat {
2021-10-29 03:14:39 +00:00
writer, err := p.printerWriter.GetWriter(nil)
if err != nil {
return err
}
2021-07-18 02:28:46 +00:00
log.Debug("Piping appendix reader...")
betterReader := bufio.NewReader(p.appendixReader)
2021-10-29 03:14:39 +00:00
_, err = io.Copy(writer, betterReader)
2021-07-18 02:28:46 +00:00
if err != nil {
return err
}
2021-10-29 03:14:39 +00:00
if err := writer.Flush(); err != nil {
return err
}
2021-07-18 02:28:46 +00:00
}
2020-11-03 23:48:43 +00:00
return nil
}