From 304fc462a41c2870650bd5fd7bf4f48817ecfe21 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 20 Feb 2022 13:15:21 +1100 Subject: [PATCH] No longer auto-read from STDIN if there are files given (#1115) --- acceptance_tests/basic.sh | 13 +++++++++---- cmd/utils.go | 6 +++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh index fbbd52cd..9e7e9c4a 100755 --- a/acceptance_tests/basic.sh +++ b/acceptance_tests/basic.sh @@ -62,11 +62,16 @@ testBasicEvalAllAllFiles() { assertEquals "$Y" "$X" } +# when given a file, don't read STDIN +# otherwise strange things start happening +# in scripts +# https://github.com/mikefarah/yq/issues/1115 + testBasicCatWithFilesNoDash() { ./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) + Y=$(./yq e '.' test2.yml) assertEquals "$Y" "$X" } @@ -74,7 +79,7 @@ testBasicEvalAllCatWithFilesNoDash() { ./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) + Y=$(./yq e '.' test2.yml) assertEquals "$Y" "$X" } @@ -82,7 +87,7 @@ testBasicCatWithFilesNoDashWithExp() { ./yq -n ".a = 123" > test.yml ./yq -n ".a = 124" > test2.yml X=$(cat test.yml | ./yq '.a' test2.yml) - Y=$(./yq e '.a' test2.yml test.yml) + Y=$(./yq e '.a' test2.yml) assertEquals "$Y" "$X" } @@ -90,7 +95,7 @@ testBasicEvalAllCatWithFilesNoDashWithExp() { ./yq -n ".a = 123" > test.yml ./yq -n ".a = 124" > test2.yml X=$(cat test.yml | ./yq ea '.a' test2.yml) - Y=$(./yq e '.a' test2.yml test.yml) + Y=$(./yq e '.a' test2.yml) assertEquals "$Y" "$X" } diff --git a/cmd/utils.go b/cmd/utils.go index ef5de85c..feff0b8e 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -117,7 +117,11 @@ func maybeFile(str string) bool { } func processStdInArgs(pipingStdin bool, args []string) []string { - if !pipingStdin { + // if we've been given a file, don't automatically + // read from stdin. + // this happens if there is more than one argument + // or only one argument and its a file + if !pipingStdin || len(args) > 1 || (len(args) > 0 && maybeFile(args[0])) { return args }