From 2891c6948d4bb0a32f2aae34a9b8f3ebd2d61675 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 7 Feb 2022 09:27:52 +1100 Subject: [PATCH] Added expression argument to forcibly set expression if required --- acceptance_tests/basic.sh | 13 +++++++++++++ cmd/constant.go | 2 ++ cmd/evaluate_all_command.go | 18 +++++------------- cmd/evalute_sequence_command.go | 21 +++++---------------- cmd/root.go | 1 + cmd/utils.go | 14 +++++++++++++- 6 files changed, 39 insertions(+), 30 deletions(-) diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh index 9aaa2000..01ef9c13 100755 --- a/acceptance_tests/basic.sh +++ b/acceptance_tests/basic.sh @@ -2,6 +2,7 @@ setUp() { rm test*.yml || true + rm .xyz -f } testBasicEvalRoundTrip() { @@ -16,6 +17,18 @@ testBasicPipeWithDot() { assertEquals "a: 123" "$X" } +testBasicExpressionMatchesFileName() { + ./yq -n ".xyz = 123" > test.yml + touch .xyz + + X=$(./yq --expression '.xyz' test.yml) + assertEquals "123" "$X" + + X=$(./yq ea --expression '.xyz' test.yml) + assertEquals "123" "$X" + +} + testBasicGitHubAction() { ./yq -n ".a = 123" > test.yml X=$(cat /dev/null | ./yq test.yml) diff --git a/cmd/constant.go b/cmd/constant.go index 5cca3c5c..31e1c66b 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -28,3 +28,5 @@ var frontMatter = "" var splitFileExp = "" var completedSuccessfully = false + +var forceExpression = "" diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index 005b2f6b..ae55b92b 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -126,27 +126,19 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) { allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator() - args = processArgs(pipingStdIn, args) + expression, args := processArgs(pipingStdIn, args) yqlib.GetLogger().Debugf("processed args: %v", args) switch len(args) { case 0: - cmd.Println(cmd.UsageString()) - return nil - case 1: if nullInput { - err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer, "") + err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(expression), printer, "") } else { - err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder) + cmd.Println(cmd.UsageString()) + return nil } default: - // 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) - } + err = allAtOnceEvaluator.EvaluateFiles(processExpression(expression), args, printer, leadingContentPreProcessing, decoder) } completedSuccessfully = err == nil diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index 77398e7a..5c19c256 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -143,29 +143,18 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) { } defer frontMatterHandler.CleanUp() } - args = processArgs(pipingStdIn, args) - yqlib.GetLogger().Debugf("processed args: %v", args) + expression, args := processArgs(pipingStdIn, args) switch len(args) { case 0: - cmd.Println(cmd.UsageString()) - return nil - case 1: if nullInput { - err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer, "") + err = streamEvaluator.EvaluateNew(processExpression(expression), printer, "") } else { - err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder) + cmd.Println(cmd.UsageString()) + return nil } default: - // 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) - } - + err = streamEvaluator.EvaluateFiles(processExpression(expression), args, printer, leadingContentPreProcessing, decoder) } completedSuccessfully = err == nil diff --git a/cmd/root.go b/cmd/root.go index 9e901efa..5fa07c28 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -81,6 +81,7 @@ yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace rootCmd.PersistentFlags().BoolVarP(&forceColor, "colors", "C", false, "force print with colors") rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") rootCmd.PersistentFlags().StringVarP(&frontMatter, "front-matter", "f", "", "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact") + rootCmd.PersistentFlags().StringVarP(&forceExpression, "expression", "", "", "forcibly set the expression argument. Useful when yq argument detection thinks your expression is a file.") rootCmd.PersistentFlags().BoolVarP(&leadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.") rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter.") diff --git a/cmd/utils.go b/cmd/utils.go index cf4e6a51..83b21d2d 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -113,7 +113,7 @@ func maybeFile(str string) bool { return result } -func processArgs(pipingStdin bool, args []string) []string { +func processStdInArgs(pipingStdin bool, args []string) []string { if !pipingStdin { return args } @@ -129,3 +129,15 @@ func processArgs(pipingStdin bool, args []string) []string { // lets add one to the end return append(args, "-") } + +func processArgs(pipingStdin bool, originalArgs []string) (string, []string) { + args := processStdInArgs(pipingStdin, originalArgs) + yqlib.GetLogger().Debugf("processed args: %v", args) + expression := forceExpression + if expression == "" && len(args) > 0 && args[0] != "-" && !maybeFile(args[0]) { + yqlib.GetLogger().Debug("assuming expression is '%v'", args[0]) + expression = args[0] + args = args[1:] + } + return expression, args +}