Added expression argument to forcibly set expression if required

This commit is contained in:
Mike Farah 2022-02-07 09:27:52 +11:00
parent 703418d0c4
commit 2891c6948d
6 changed files with 39 additions and 30 deletions

View File

@ -2,6 +2,7 @@
setUp() { setUp() {
rm test*.yml || true rm test*.yml || true
rm .xyz -f
} }
testBasicEvalRoundTrip() { testBasicEvalRoundTrip() {
@ -16,6 +17,18 @@ testBasicPipeWithDot() {
assertEquals "a: 123" "$X" 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() { testBasicGitHubAction() {
./yq -n ".a = 123" > test.yml ./yq -n ".a = 123" > test.yml
X=$(cat /dev/null | ./yq test.yml) X=$(cat /dev/null | ./yq test.yml)

View File

@ -28,3 +28,5 @@ var frontMatter = ""
var splitFileExp = "" var splitFileExp = ""
var completedSuccessfully = false var completedSuccessfully = false
var forceExpression = ""

View File

@ -126,27 +126,19 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator() allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
args = processArgs(pipingStdIn, args) expression, args := processArgs(pipingStdIn, args)
yqlib.GetLogger().Debugf("processed args: %v", args) yqlib.GetLogger().Debugf("processed args: %v", args)
switch len(args) { switch len(args) {
case 0: case 0:
cmd.Println(cmd.UsageString())
return nil
case 1:
if nullInput { if nullInput {
err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer, "") err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(expression), printer, "")
} else { } else {
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder) cmd.Println(cmd.UsageString())
return nil
} }
default: default:
// the first argument is either an expression - or a file. err = allAtOnceEvaluator.EvaluateFiles(processExpression(expression), args, printer, leadingContentPreProcessing, decoder)
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 completedSuccessfully = err == nil

View File

@ -143,29 +143,18 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
} }
defer frontMatterHandler.CleanUp() defer frontMatterHandler.CleanUp()
} }
args = processArgs(pipingStdIn, args) expression, args := processArgs(pipingStdIn, args)
yqlib.GetLogger().Debugf("processed args: %v", args)
switch len(args) { switch len(args) {
case 0: case 0:
cmd.Println(cmd.UsageString())
return nil
case 1:
if nullInput { if nullInput {
err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer, "") err = streamEvaluator.EvaluateNew(processExpression(expression), printer, "")
} else { } else {
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder) cmd.Println(cmd.UsageString())
return nil
} }
default: default:
// the first argument is either an expression - or a file. err = streamEvaluator.EvaluateFiles(processExpression(expression), args, printer, leadingContentPreProcessing, decoder)
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 completedSuccessfully = err == nil

View File

@ -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(&forceColor, "colors", "C", false, "force print with colors")
rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no 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(&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().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.") 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.")

View File

@ -113,7 +113,7 @@ func maybeFile(str string) bool {
return result return result
} }
func processArgs(pipingStdin bool, args []string) []string { func processStdInArgs(pipingStdin bool, args []string) []string {
if !pipingStdin { if !pipingStdin {
return args return args
} }
@ -129,3 +129,15 @@ func processArgs(pipingStdin bool, args []string) []string {
// lets add one to the end // lets add one to the end
return append(args, "-") 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
}