Allow multiple files with no expression

This commit is contained in:
Mike Farah 2022-02-07 08:20:01 +11:00
parent 5ac3c57510
commit 75960c6484
3 changed files with 66 additions and 3 deletions

View File

@ -13,7 +13,56 @@ testBasicEvalRoundTrip() {
testBasicPipeWithDot() {
./yq -n ".a = 123" > test.yml
X=$(cat test.yml | ./yq '.')
assertEquals 123 "$X"
assertEquals "a: 123" "$X"
}
testBasicAllFiles() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(./yq test.yml test2.yml)
Y=$(./yq e '.' test.yml test2.yml)
assertEquals "$Y" "$X"
}
testBasicEvalAllAllFiles() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(./yq ea test.yml test2.yml)
Y=$(./yq e '.' test.yml test2.yml)
assertEquals "$Y" "$X"
}
testBasicStdInWithFiles() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(cat test.yml | ./yq - test2.yml)
Y=$(./yq e '.' test.yml test2.yml)
assertEquals "$Y" "$X"
}
testBasicEvalAllStdInWithFiles() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(cat test.yml | ./yq ea - test2.yml)
Y=$(./yq e '.' test.yml test2.yml)
assertEquals "$Y" "$X"
}
testBasicStdInWithFilesReverse() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(cat test.yml | ./yq test2.yml -)
Y=$(./yq e '.' test2.yml test.yml)
assertEquals "$Y" "$X"
}
testBasicEvalAllStdInWithFilesReverse() {
./yq -n ".a = 123" > test.yml
./yq -n ".a = 124" > test2.yml
X=$(cat test.yml | ./yq ea test2.yml -)
Y=$(./yq e '.' test2.yml test.yml)
assertEquals "$Y" "$X"
}
testBasicEvalRoundTripNoEval() {

View File

@ -144,7 +144,13 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
}
default:
err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer, leadingContentPreProcessing, decoder)
// the first argument is either an expression - or a file.
if args[0] == "-" || maybeFile(args[0]) {
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), args, printer, leadingContentPreProcessing, decoder)
} else {
// first argument is an expression, the rest are files.
err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer, leadingContentPreProcessing, decoder)
}
}
completedSuccessfully = err == nil

View File

@ -162,7 +162,15 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
}
default:
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer, leadingContentPreProcessing, decoder)
// the first argument is either an expression - or a file.
if args[0] == "-" || maybeFile(args[0]) {
// its a file, there is no expression given
err = streamEvaluator.EvaluateFiles(processExpression(""), args, printer, leadingContentPreProcessing, decoder)
} else {
// first argument is an expression, the rest are files.
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer, leadingContentPreProcessing, decoder)
}
}
completedSuccessfully = err == nil