yq/pkg/yqlib/utils.go
2021-02-11 10:58:40 +11:00

50 lines
956 B
Go

package yqlib
import (
"bufio"
"container/list"
"io"
"os"
yaml "gopkg.in/yaml.v3"
)
func readStream(filename string) (io.Reader, error) {
if filename == "-" {
return bufio.NewReader(os.Stdin), nil
} else {
return os.Open(filename) // nolint gosec
}
}
func readDocuments(reader io.Reader, filename string, fileIndex int) (*list.List, error) {
decoder := yaml.NewDecoder(reader)
inputList := list.New()
var currentIndex uint = 0
for {
var dataBucket yaml.Node
errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF {
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader)
}
return inputList, nil
} else if errorReading != nil {
return nil, errorReading
}
candidateNode := &CandidateNode{
Document: currentIndex,
Filename: filename,
Node: &dataBucket,
FileIndex: fileIndex,
}
inputList.PushBack(candidateNode)
currentIndex = currentIndex + 1
}
}