2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"container/list"
|
2021-11-22 06:43:38 +00:00
|
|
|
"errors"
|
2020-11-03 23:48:43 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2021-07-19 09:52:51 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-11-03 23:48:43 +00:00
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2021-07-19 23:18:40 +00:00
|
|
|
func readStream(filename string, leadingContentPreProcessing bool) (io.Reader, string, error) {
|
2021-07-19 09:52:51 +00:00
|
|
|
var reader *bufio.Reader
|
2020-11-03 23:48:43 +00:00
|
|
|
if filename == "-" {
|
2021-07-19 09:52:51 +00:00
|
|
|
reader = bufio.NewReader(os.Stdin)
|
2020-11-03 23:48:43 +00:00
|
|
|
} else {
|
2021-07-08 00:26:35 +00:00
|
|
|
// ignore CWE-22 gosec issue - that's more targetted for http based apps that run in a public directory,
|
|
|
|
// and ensuring that it's not possible to give a path to a file outside thar directory.
|
2021-07-19 09:52:51 +00:00
|
|
|
file, err := os.Open(filename) // #nosec
|
2021-07-14 10:48:16 +00:00
|
|
|
if err != nil {
|
2021-07-19 09:52:51 +00:00
|
|
|
return nil, "", err
|
2021-07-14 10:48:16 +00:00
|
|
|
}
|
2021-07-19 09:52:51 +00:00
|
|
|
reader = bufio.NewReader(file)
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:18:40 +00:00
|
|
|
if !leadingContentPreProcessing {
|
|
|
|
return reader, "", nil
|
|
|
|
}
|
2021-07-20 00:19:55 +00:00
|
|
|
return processReadStream(reader)
|
|
|
|
}
|
2021-07-19 23:18:40 +00:00
|
|
|
|
2021-11-12 04:02:28 +00:00
|
|
|
func writeString(writer io.Writer, txt string) error {
|
|
|
|
_, errorWriting := writer.Write([]byte(txt))
|
|
|
|
return errorWriting
|
|
|
|
}
|
|
|
|
|
|
|
|
func processLeadingContent(mappedDoc *CandidateNode, writer io.Writer, printDocSeparators bool, outputFormat PrinterOutputFormat) error {
|
2021-11-13 23:31:37 +00:00
|
|
|
log.Debug("headcommentwas %v", mappedDoc.LeadingContent)
|
|
|
|
log.Debug("finished headcomment")
|
|
|
|
reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent))
|
2021-11-12 04:02:28 +00:00
|
|
|
|
2021-11-13 23:31:37 +00:00
|
|
|
for {
|
2021-11-12 04:02:28 +00:00
|
|
|
|
2021-11-13 23:31:37 +00:00
|
|
|
readline, errReading := reader.ReadString('\n')
|
2021-11-22 06:43:38 +00:00
|
|
|
if errReading != nil && !errors.Is(errReading, io.EOF) {
|
2021-11-13 23:31:37 +00:00
|
|
|
return errReading
|
|
|
|
}
|
|
|
|
if strings.Contains(readline, "$yqDocSeperator$") {
|
|
|
|
if printDocSeparators {
|
|
|
|
if err := writeString(writer, "---\n"); err != nil {
|
2021-11-12 04:02:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 23:31:37 +00:00
|
|
|
} else if outputFormat == YamlOutputFormat {
|
|
|
|
if err := writeString(writer, readline); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 04:02:28 +00:00
|
|
|
|
2021-11-22 06:43:38 +00:00
|
|
|
if errors.Is(errReading, io.EOF) {
|
2021-11-13 23:31:37 +00:00
|
|
|
if readline != "" {
|
|
|
|
// the last comment we read didn't have a new line, put one in
|
|
|
|
if err := writeString(writer, "\n"); err != nil {
|
|
|
|
return err
|
2021-11-12 04:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 23:31:37 +00:00
|
|
|
break
|
2021-11-12 04:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-13 23:31:37 +00:00
|
|
|
|
2021-11-12 04:02:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:19:55 +00:00
|
|
|
func processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
|
|
|
|
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
|
|
|
var sb strings.Builder
|
2021-07-19 09:52:51 +00:00
|
|
|
for {
|
|
|
|
peekBytes, err := reader.Peek(3)
|
2021-11-22 06:43:38 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-07-16 10:56:22 +00:00
|
|
|
// EOF are handled else where..
|
2021-07-19 09:52:51 +00:00
|
|
|
return reader, sb.String(), nil
|
2021-07-16 10:56:22 +00:00
|
|
|
} else if err != nil {
|
2021-07-19 09:52:51 +00:00
|
|
|
return reader, sb.String(), err
|
2021-07-20 00:19:55 +00:00
|
|
|
} else if string(peekBytes) == "---" {
|
|
|
|
_, err := reader.ReadString('\n')
|
|
|
|
sb.WriteString("$yqDocSeperator$\n")
|
2021-11-22 06:43:38 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-07-20 00:19:55 +00:00
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
|
|
|
} else if commentLineRegEx.MatchString(string(peekBytes)) {
|
2021-07-19 09:52:51 +00:00
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
sb.WriteString(line)
|
2021-11-22 06:43:38 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-07-19 09:52:51 +00:00
|
|
|
return reader, sb.String(), nil
|
|
|
|
} else if err != nil {
|
|
|
|
return reader, sb.String(), err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return reader, sb.String(), nil
|
2021-07-14 10:48:16 +00:00
|
|
|
}
|
2020-11-06 03:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:57:32 +00:00
|
|
|
func readDocuments(reader io.Reader, filename string, fileIndex int) (*list.List, error) {
|
2020-11-13 02:19:54 +00:00
|
|
|
decoder := yaml.NewDecoder(reader)
|
|
|
|
inputList := list.New()
|
|
|
|
var currentIndex uint = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
var dataBucket yaml.Node
|
|
|
|
errorReading := decoder.Decode(&dataBucket)
|
|
|
|
|
2021-11-22 06:43:38 +00:00
|
|
|
if errors.Is(errorReading, io.EOF) {
|
2020-11-13 03:07:11 +00:00
|
|
|
switch reader := reader.(type) {
|
2020-11-13 02:19:54 +00:00
|
|
|
case *os.File:
|
2020-11-13 03:07:11 +00:00
|
|
|
safelyCloseFile(reader)
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
return inputList, nil
|
|
|
|
} else if errorReading != nil {
|
|
|
|
return nil, errorReading
|
|
|
|
}
|
|
|
|
candidateNode := &CandidateNode{
|
2021-01-17 23:15:31 +00:00
|
|
|
Document: currentIndex,
|
|
|
|
Filename: filename,
|
|
|
|
Node: &dataBucket,
|
|
|
|
FileIndex: fileIndex,
|
|
|
|
EvaluateTogether: true,
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
2020-11-06 01:11:38 +00:00
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
inputList.PushBack(candidateNode)
|
|
|
|
|
|
|
|
currentIndex = currentIndex + 1
|
|
|
|
}
|
|
|
|
}
|