2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-03-28 22:51:55 +00:00
|
|
|
"bytes"
|
2020-11-03 23:48:43 +00:00
|
|
|
"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"
|
2024-02-24 03:58:11 +00:00
|
|
|
"strings"
|
2020-11-03 23:48:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Printer interface {
|
2021-07-20 00:19:55 +00:00
|
|
|
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)
|
2023-03-28 22:51:55 +00:00
|
|
|
SetNulSepOutput(nulSepOutput bool)
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 03:58:11 +00:00
|
|
|
type PrinterOutputFormat struct {
|
|
|
|
FormalName string
|
|
|
|
Names []string
|
|
|
|
}
|
2021-07-25 08:08:33 +00:00
|
|
|
|
2024-02-24 03:58:11 +00:00
|
|
|
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}}
|
|
|
|
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}}
|
|
|
|
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}}
|
|
|
|
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}}
|
|
|
|
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}}
|
|
|
|
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}}
|
|
|
|
|
|
|
|
var Base64OutputFormat = &PrinterOutputFormat{}
|
|
|
|
var UriOutputFormat = &PrinterOutputFormat{}
|
|
|
|
var ShOutputFormat = &PrinterOutputFormat{}
|
|
|
|
|
|
|
|
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}}
|
|
|
|
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}}
|
|
|
|
|
|
|
|
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}}
|
|
|
|
|
|
|
|
var Formats = []*PrinterOutputFormat{
|
|
|
|
YamlOutputFormat,
|
|
|
|
JSONOutputFormat,
|
|
|
|
PropsOutputFormat,
|
|
|
|
CSVOutputFormat,
|
|
|
|
TSVOutputFormat,
|
|
|
|
XMLOutputFormat,
|
|
|
|
Base64OutputFormat,
|
|
|
|
UriOutputFormat,
|
|
|
|
ShOutputFormat,
|
|
|
|
TomlOutputFormat,
|
|
|
|
ShellVariablesOutputFormat,
|
|
|
|
LuaOutputFormat,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *PrinterOutputFormat) MatchesName(name string) bool {
|
|
|
|
if f.FormalName == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, n := range f.Names {
|
|
|
|
if n == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func OutputFormatFromString(format string) (*PrinterOutputFormat, error) {
|
|
|
|
for _, printerFormat := range Formats {
|
|
|
|
if printerFormat.MatchesName(format) {
|
|
|
|
return printerFormat, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown format '%v' please use [%v]", format, GetAvailableOutputFormatString())
|
|
|
|
}
|
2021-07-25 08:08:33 +00:00
|
|
|
|
2024-02-24 03:58:11 +00:00
|
|
|
func GetAvailableOutputFormatString() string {
|
|
|
|
var formats = []string{}
|
|
|
|
for _, printerFormat := range Formats {
|
|
|
|
if printerFormat.FormalName != "" {
|
|
|
|
formats = append(formats, printerFormat.FormalName)
|
|
|
|
}
|
|
|
|
if len(printerFormat.Names) >= 1 {
|
|
|
|
formats = append(formats, printerFormat.Names[0])
|
|
|
|
}
|
2021-07-25 08:08:33 +00:00
|
|
|
}
|
2024-02-24 03:58:11 +00:00
|
|
|
return strings.Join(formats, "|")
|
2021-07-25 08:08:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:48:43 +00:00
|
|
|
type resultsPrinter struct {
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder Encoder
|
|
|
|
printerWriter PrinterWriter
|
|
|
|
firstTimePrinting bool
|
|
|
|
previousDocIndex uint
|
|
|
|
previousFileIndex int
|
|
|
|
printedMatches bool
|
|
|
|
treeNavigator DataTreeNavigator
|
|
|
|
appendixReader io.Reader
|
2023-03-28 22:51:55 +00:00
|
|
|
nulSepOutput bool
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func NewPrinter(encoder Encoder, printerWriter PrinterWriter) Printer {
|
2020-11-13 02:19:54 +00:00
|
|
|
return &resultsPrinter{
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder: encoder,
|
|
|
|
printerWriter: printerWriter,
|
|
|
|
firstTimePrinting: true,
|
|
|
|
treeNavigator: NewDataTreeNavigator(),
|
2023-03-28 22:51:55 +00:00
|
|
|
nulSepOutput: false,
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 22:51:55 +00:00
|
|
|
func (p *resultsPrinter) SetNulSepOutput(nulSepOutput bool) {
|
|
|
|
log.Debug("Setting NUL separator output")
|
|
|
|
|
|
|
|
p.nulSepOutput = nulSepOutput
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (p *resultsPrinter) printNode(node *CandidateNode, 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"))
|
2022-01-15 00:57:59 +00:00
|
|
|
return p.encoder.Encode(writer, node)
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 22:51:55 +00:00
|
|
|
func removeLastEOL(b *bytes.Buffer) {
|
|
|
|
data := b.Bytes()
|
|
|
|
n := len(data)
|
|
|
|
if n >= 2 && data[n-2] == '\r' && data[n-1] == '\n' {
|
|
|
|
b.Truncate(n - 2)
|
|
|
|
} else if n >= 1 && (data[n-1] == '\r' || data[n-1] == '\n') {
|
|
|
|
b.Truncate(n - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:19:55 +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())
|
2021-10-29 03:14:39 +00:00
|
|
|
|
|
|
|
if matchingNodes.Len() == 0 {
|
|
|
|
log.Debug("no matching results, nothing to print")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
if !p.encoder.CanHandleAliases() {
|
2021-01-11 06:13:48 +00:00
|
|
|
explodeOp := Operation{OperationType: explodeOpType}
|
2021-01-12 23:18:53 +00:00
|
|
|
explodeNode := ExpressionNode{Operation: &explodeOp}
|
2021-02-02 07:17:59 +00:00
|
|
|
context, err := p.treeNavigator.GetMatchingNodes(Context{MatchingNodes: matchingNodes}, &explodeNode)
|
2020-11-06 03:37:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
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)
|
2023-10-18 01:11:53 +00:00
|
|
|
p.previousDocIndex = node.GetDocument()
|
|
|
|
p.previousFileIndex = node.GetFileIndex()
|
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)
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debug("print sep logic: p.firstTimePrinting: %v, previousDocIndex: %v", p.firstTimePrinting, p.previousDocIndex)
|
2022-10-28 03:16:46 +00:00
|
|
|
log.Debug("%v", NodeToString(mappedDoc))
|
2021-10-29 03:14:39 +00:00
|
|
|
writer, errorWriting := p.printerWriter.GetWriter(mappedDoc)
|
|
|
|
if errorWriting != nil {
|
|
|
|
return errorWriting
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
commentsStartWithSepExp := regexp.MustCompile(`^\$yqDocSeparator\$`)
|
2021-11-13 23:31:37 +00:00
|
|
|
commentStartsWithSeparator := commentsStartWithSepExp.MatchString(mappedDoc.LeadingContent)
|
2021-07-20 00:19:55 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if (p.previousDocIndex != mappedDoc.GetDocument() || p.previousFileIndex != mappedDoc.GetFileIndex()) && !commentStartsWithSeparator {
|
2022-01-15 00:57:59 +00:00
|
|
|
if err := p.encoder.PrintDocumentSeparator(writer); err != nil {
|
2020-11-13 03:07:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 22:51:55 +00:00
|
|
|
var destination io.Writer = writer
|
|
|
|
tempBuffer := bytes.NewBuffer(nil)
|
|
|
|
if p.nulSepOutput {
|
|
|
|
destination = tempBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.encoder.PrintLeadingContent(destination, mappedDoc.LeadingContent); err != nil {
|
2021-10-29 03:14:39 +00:00
|
|
|
return err
|
2021-07-20 00:19:55 +00:00
|
|
|
}
|
2021-07-25 08:08:33 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if err := p.printNode(mappedDoc, destination); err != nil {
|
2022-05-25 00:54:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:51:55 +00:00
|
|
|
if p.nulSepOutput {
|
|
|
|
removeLastEOL(tempBuffer)
|
|
|
|
tempBufferBytes := tempBuffer.Bytes()
|
|
|
|
if bytes.IndexByte(tempBufferBytes, 0) != -1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Can't serialize value because it contains NUL char and you are using NUL separated output",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if _, err := writer.Write(tempBufferBytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := writer.Write([]byte{0}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
p.previousDocIndex = mappedDoc.GetDocument()
|
2021-10-29 03:14:39 +00:00
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
log.Debugf("done printing results")
|
2020-11-03 23:48:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
// what happens if I remove output format check?
|
|
|
|
if p.appendixReader != nil {
|
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
|
|
|
|
}
|