yq/pkg/yqlib/utils.go

64 lines
1.4 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
import (
"bufio"
"container/list"
2021-11-22 06:43:38 +00:00
"errors"
"fmt"
2020-11-03 23:48:43 +00:00
"io"
"os"
)
2022-10-25 03:22:16 +00:00
func readStream(filename string) (io.Reader, 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-11-25 09:24:51 +00:00
// ignore CWE-22 gosec issue - that's more targeted 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
if err != nil {
2022-10-25 03:22:16 +00:00
return nil, err
}
2021-07-19 09:52:51 +00:00
reader = bufio.NewReader(file)
}
2022-10-25 03:22:16 +00:00
return reader, nil
2021-07-19 09:52:51 +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
}
2021-12-21 04:02:07 +00:00
func readDocuments(reader io.Reader, filename string, fileIndex int, decoder Decoder) (*list.List, error) {
err := decoder.Init(reader)
if err != nil {
return nil, err
}
2020-11-13 02:19:54 +00:00
inputList := list.New()
var currentIndex uint
2020-11-13 02:19:54 +00:00
for {
2022-10-25 03:22:16 +00:00
candidateNode, errorReading := decoder.Decode()
2020-11-13 02:19:54 +00:00
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, fmt.Errorf("bad file '%v': %w", filename, errorReading)
2020-11-13 02:19:54 +00:00
}
2022-10-25 03:22:16 +00:00
candidateNode.Document = currentIndex
candidateNode.Filename = filename
candidateNode.FileIndex = fileIndex
candidateNode.EvaluateTogether = true
2020-11-13 02:19:54 +00:00
inputList.PushBack(candidateNode)
currentIndex = currentIndex + 1
}
}