yq/pkg/yqlib/utils.go

186 lines
4.3 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"
)
2020-11-06 01:11:38 +00:00
var treeNavigator = NewDataTreeNavigator(NavigationPrefs{})
2020-11-06 03:37:01 +00:00
var treeCreator = NewPathTreeCreator()
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-13 02:19:54 +00:00
func EvaluateStream(filename string, reader io.Reader, node *PathTreeNode, printer Printer) error {
2020-11-03 23:48:43 +00:00
var currentIndex uint = 0
2020-11-06 01:11:38 +00:00
decoder := yaml.NewDecoder(reader)
2020-11-03 23:48:43 +00:00
for {
var dataBucket yaml.Node
errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF {
2020-11-13 02:19:54 +00:00
return nil
2020-11-03 23:48:43 +00:00
} else if errorReading != nil {
2020-11-13 02:19:54 +00:00
return errorReading
2020-11-03 23:48:43 +00:00
}
candidateNode := &CandidateNode{
Document: currentIndex,
Filename: filename,
Node: &dataBucket,
}
inputList := list.New()
inputList.PushBack(candidateNode)
2020-11-13 02:19:54 +00:00
matches, errorParsing := treeNavigator.GetMatchingNodes(inputList, node)
2020-11-03 23:48:43 +00:00
if errorParsing != nil {
2020-11-13 02:19:54 +00:00
return errorParsing
2020-11-03 23:48:43 +00:00
}
2020-11-13 03:07:11 +00:00
err := printer.PrintResults(matches)
if err != nil {
return err
}
2020-11-03 23:48:43 +00:00
currentIndex = currentIndex + 1
}
2020-11-06 01:11:38 +00:00
}
2020-11-13 02:19:54 +00:00
func readDocuments(reader io.Reader, filename string) (*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 {
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{
Document: currentIndex,
Filename: filename,
Node: &dataBucket,
}
2020-11-06 01:11:38 +00:00
2020-11-13 02:19:54 +00:00
inputList.PushBack(candidateNode)
currentIndex = currentIndex + 1
}
}
func EvaluateAllFileStreams(expression string, filenames []string, printer Printer) error {
node, err := treeCreator.ParsePath(expression)
if err != nil {
return err
}
var allDocuments *list.List = list.New()
for _, filename := range filenames {
reader, err := readStream(filename)
if err != nil {
return err
}
fileDocuments, err := readDocuments(reader, filename)
if err != nil {
return err
}
allDocuments.PushBackList(fileDocuments)
}
matches, err := treeNavigator.GetMatchingNodes(allDocuments, node)
2020-11-06 01:11:38 +00:00
if err != nil {
2020-11-13 02:19:54 +00:00
return err
2020-11-06 01:11:38 +00:00
}
2020-11-13 02:19:54 +00:00
return printer.PrintResults(matches)
}
func EvaluateFileStreamsSequence(expression string, filenames []string, printer Printer) error {
2020-11-03 23:48:43 +00:00
2020-11-13 02:19:54 +00:00
node, err := treeCreator.ParsePath(expression)
if err != nil {
return err
}
for _, filename := range filenames {
reader, err := readStream(filename)
if err != nil {
return err
}
err = EvaluateStream(filename, reader, node, printer)
if err != nil {
return err
}
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 nil
2020-11-03 23:48:43 +00:00
}
2020-11-13 03:07:11 +00:00
// func safelyRenameFile(from string, to string) {
// if renameError := os.Rename(from, to); renameError != nil {
// log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
// log.Debug(renameError.Error())
// // can't do this rename when running in docker to a file targeted in a mounted volume,
// // so gracefully degrade to copying the entire contents.
// if copyError := copyFileContents(from, to); copyError != nil {
// log.Errorf("Failed copying from %v to %v", from, to)
// log.Error(copyError.Error())
// } else {
// removeErr := os.Remove(from)
// if removeErr != nil {
// log.Errorf("failed removing original file: %s", from)
// }
// }
// }
// }
2020-11-13 09:58:01 +00:00
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
2020-11-13 10:22:05 +00:00
// func copyFileContents(src, dst string) (err error) {
// in, err := os.Open(src) // nolint gosec
// if err != nil {
// return err
// }
// defer safelyCloseFile(in)
// out, err := os.Create(dst)
// if err != nil {
// return err
// }
// defer safelyCloseFile(out)
// if _, err = io.Copy(out, in); err != nil {
// return err
// }
// return out.Sync()
// }
2020-11-03 23:48:43 +00:00
func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil {
log.Error("Error flushing writer!")
log.Error(err.Error())
}
}
func safelyCloseFile(file *os.File) {
err := file.Close()
if err != nil {
log.Error("Error closing file!")
log.Error(err.Error())
}
}