yq/pkg/yqlib/utils.go

51 lines
1011 B
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
import (
"bufio"
"container/list"
"io"
"os"
yaml "gopkg.in/yaml.v3"
)
2020-11-06 01:11:38 +00:00
func readStream(filename string) (io.Reader, error) {
2020-11-03 23:48:43 +00:00
if filename == "-" {
2020-11-13 02:19:54 +00:00
return bufio.NewReader(os.Stdin), nil
2020-11-03 23:48:43 +00:00
} else {
2020-11-13 02:19:54 +00:00
return os.Open(filename) // nolint gosec
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)
if 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
}
}