Smarter behaviour when piping data in with one argument

This commit is contained in:
Mike Farah 2022-01-27 10:18:46 +11:00
parent c3d815998a
commit fc276ff450
4 changed files with 18 additions and 2 deletions

View File

@ -10,6 +10,15 @@ testBasicEvalRoundTrip() {
assertEquals 123 "$X"
}
testBasicStdInWithOneArg() {
./yq e -n ".a = 123" > test.yml
X=$(cat test.yml | ./yq e ".a")
assertEquals 123 "$X"
X=$(cat test.yml | ./yq ea ".a")
assertEquals 123 "$X"
}
testBasicUpdateInPlaceSequence() {
cat >test.yml <<EOL
a: 0

View File

@ -120,6 +120,10 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
case 1:
if nullInput {
err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer, "")
} else if pipingStdIn && args[0] != "-" {
// must have given a single expression and piping input from stdin
err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), []string{"-"}, printer, leadingContentPreProcessing, decoder)
} else {
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
}

View File

@ -134,6 +134,9 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
case 1:
if nullInput {
err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer, "")
} else if pipingStdIn && args[0] != "-" {
// must have given a single expression and piping input from stdin
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), []string{"-"}, printer, leadingContentPreProcessing, decoder)
} else {
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
}

View File

@ -14,13 +14,13 @@ func New() *cobra.Command {
Short: "yq is a lightweight and portable command-line YAML processor.",
Long: `yq is a portable command-line YAML processor (https://github.com/mikefarah/yq/)
See https://mikefarah.gitbook.io/yq/ for detailed documentation and examples.`,
RunE: func(cmd *cobra.Command, args []string) error {
if version {
cmd.Print(GetVersionDisplay())
return nil
}
cmd.Println(cmd.UsageString())
return nil
return evaluateSequence(cmd, args)
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {