yq/pkg/yqlib/utils.go

77 lines
1.7 KiB
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"
)
func readStream(filename string) (io.Reader, bool, error) {
2020-11-03 23:48:43 +00:00
if filename == "-" {
reader := bufio.NewReader(os.Stdin)
seperatorBytes, err := reader.Peek(3)
if err == io.EOF {
// EOF are handled else where..
return reader, false, nil
}
return reader, string(seperatorBytes) == "---", err
2020-11-03 23:48:43 +00:00
} else {
// 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-16 00:09:12 +00:00
reader, err := os.Open(filename) // #nosec
if err != nil {
return nil, false, err
}
seperatorBytes := make([]byte, 3)
_, err = reader.Read(seperatorBytes)
if err == io.EOF {
// EOF are handled else where..
return reader, false, nil
} else if err != nil {
return nil, false, err
}
_, err = reader.Seek(0, 0)
return reader, string(seperatorBytes) == "---", err
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
}
}