Merge branch 'master' into xml-comments

This commit is contained in:
Mike Farah
2022-01-15 10:42:25 +11:00
17 changed files with 466 additions and 36 deletions
+7 -9
View File
@@ -1,30 +1,28 @@
package yqlib
import (
"fmt"
"io"
"io/ioutil"
"os"
)
func safelyRenameFile(from string, to string) {
func tryRenameFile(from string, to string) error {
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())
log.Debug("going to try copying instead")
// 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)
}
return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)
}
tryRemoveTempFile(from)
}
return nil
}
func tryRemoveFile(filename string) {
func tryRemoveTempFile(filename string) {
log.Debug("Removing temp file: %v", filename)
removeErr := os.Remove(filename)
if removeErr != nil {
+1 -1
View File
@@ -33,7 +33,7 @@ func (f *frontMatterHandlerImpl) GetContentReader() io.Reader {
}
func (f *frontMatterHandlerImpl) CleanUp() {
tryRemoveFile(f.yamlFrontMatterFilename)
tryRemoveTempFile(f.yamlFrontMatterFilename)
}
// Splits the given file by yaml front matter
+3 -3
View File
@@ -66,7 +66,7 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}
@@ -103,7 +103,7 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}
@@ -137,6 +137,6 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}
+12 -4
View File
@@ -1,5 +1,7 @@
package yqlib
import "container/list"
func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debug("unionOperator")
log.Debug("context: %v", NodesToString(context.MatchingNodes))
@@ -18,19 +20,25 @@ func unionOperator(d *dataTreeNavigator, context Context, expressionNode *Expres
log.Debug("lhs: %v", lhs.ToString())
log.Debug("rhs: %v", rhs.ToString())
results := lhs.ChildContext(list.New())
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
results.MatchingNodes.PushBack(node)
}
// this can happen when both expressions modify the context
// instead of creating their own.
/// (.foo = "bar"), (.thing = "cat")
if rhs.MatchingNodes != lhs.MatchingNodes {
for el := rhs.MatchingNodes.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
log.Debug("processing %v", NodeToString(node))
lhs.MatchingNodes.PushBack(node)
results.MatchingNodes.PushBack(node)
}
}
log.Debug("all together: %v", lhs.ToString())
return lhs, nil
log.Debug("and lets print it out")
log.Debug("all together: %v", results.ToString())
return results, nil
}
+8
View File
@@ -13,6 +13,14 @@ var unionOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::{}\n",
},
},
{
skipDoc: true,
description: "clone test",
expression: `"abc" as $a | [$a, "cat"]`,
expected: []string{
"D0, P[], (!!seq)::- abc\n- cat\n",
},
},
{
skipDoc: true,
expression: `(.foo = "bar"), (.toe = "jam")`,
+6 -5
View File
@@ -6,7 +6,7 @@ import (
type writeInPlaceHandler interface {
CreateTempFile() (*os.File, error)
FinishWriteInPlace(evaluatedSuccessfully bool)
FinishWriteInPlace(evaluatedSuccessfully bool) error
}
type writeInPlaceHandlerImpl struct {
@@ -39,13 +39,14 @@ func (w *writeInPlaceHandlerImpl) CreateTempFile() (*os.File, error) {
return file, err
}
func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) {
func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) error {
log.Debug("Going to write-inplace, evaluatedSuccessfully=%v, target=%v", evaluatedSuccessfully, w.inputFilename)
safelyCloseFile(w.tempFile)
if evaluatedSuccessfully {
log.Debug("Moving temp file to target")
safelyRenameFile(w.tempFile.Name(), w.inputFilename)
} else {
tryRemoveFile(w.tempFile.Name())
return tryRenameFile(w.tempFile.Name(), w.inputFilename)
}
tryRemoveTempFile(w.tempFile.Name())
return nil
}