mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 19:25:37 +00:00
Can load expressions from file #1120
This commit is contained in:
parent
edbdb16ab7
commit
33a29817d7
@ -3,6 +3,7 @@
|
|||||||
setUp() {
|
setUp() {
|
||||||
rm test*.yml 2>/dev/null || true
|
rm test*.yml 2>/dev/null || true
|
||||||
rm .xyz 2>/dev/null || true
|
rm .xyz 2>/dev/null || true
|
||||||
|
rm instructions.txt 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
testBasicEvalRoundTrip() {
|
testBasicEvalRoundTrip() {
|
||||||
@ -26,7 +27,17 @@ testBasicExpressionMatchesFileName() {
|
|||||||
|
|
||||||
X=$(./yq ea --expression '.xyz' test.yml)
|
X=$(./yq ea --expression '.xyz' test.yml)
|
||||||
assertEquals "123" "$X"
|
assertEquals "123" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicExpressionFromFile() {
|
||||||
|
./yq -n ".xyz = 123" > test.yml
|
||||||
|
echo '.xyz = "meow" | .cool = "frog"' > instructions.txt
|
||||||
|
|
||||||
|
X=$(./yq --from-file instructions.txt test.yml -o=j -I=0)
|
||||||
|
assertEquals '{"xyz":"meow","cool":"frog"}' "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea --from-file instructions.txt test.yml -o=j -I=0)
|
||||||
|
assertEquals '{"xyz":"meow","cool":"frog"}' "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
testBasicGitHubAction() {
|
testBasicGitHubAction() {
|
||||||
|
@ -30,3 +30,5 @@ var splitFileExp = ""
|
|||||||
var completedSuccessfully = false
|
var completedSuccessfully = false
|
||||||
|
|
||||||
var forceExpression = ""
|
var forceExpression = ""
|
||||||
|
|
||||||
|
var expressionFile = ""
|
||||||
|
@ -126,7 +126,10 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
|
|
||||||
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
||||||
|
|
||||||
expression, args := processArgs(pipingStdIn, args)
|
expression, args, err := processArgs(pipingStdIn, args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
yqlib.GetLogger().Debugf("processed args: %v", args)
|
yqlib.GetLogger().Debugf("processed args: %v", args)
|
||||||
|
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
|
@ -143,7 +143,10 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
}
|
}
|
||||||
defer frontMatterHandler.CleanUp()
|
defer frontMatterHandler.CleanUp()
|
||||||
}
|
}
|
||||||
expression, args := processArgs(pipingStdIn, args)
|
expression, args, err := processArgs(pipingStdIn, args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -86,6 +86,8 @@ yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace
|
|||||||
|
|
||||||
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.")
|
||||||
|
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&expressionFile, "from-file", "", "", "Load expression from specified file.")
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
createEvaluateSequenceCommand(),
|
createEvaluateSequenceCommand(),
|
||||||
createEvaluateAllCommand(),
|
createEvaluateAllCommand(),
|
||||||
|
14
cmd/utils.go
14
cmd/utils.go
@ -137,14 +137,22 @@ func processStdInArgs(pipingStdin bool, args []string) []string {
|
|||||||
return append(args, "-")
|
return append(args, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
func processArgs(pipingStdin bool, originalArgs []string) (string, []string) {
|
func processArgs(pipingStdin bool, originalArgs []string) (string, []string, error) {
|
||||||
|
expression := forceExpression
|
||||||
|
if expressionFile != "" {
|
||||||
|
expressionBytes, err := os.ReadFile(expressionFile)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
expression = string(expressionBytes)
|
||||||
|
}
|
||||||
|
|
||||||
args := processStdInArgs(pipingStdin, originalArgs)
|
args := processStdInArgs(pipingStdin, originalArgs)
|
||||||
yqlib.GetLogger().Debugf("processed args: %v", args)
|
yqlib.GetLogger().Debugf("processed args: %v", args)
|
||||||
expression := forceExpression
|
|
||||||
if expression == "" && len(args) > 0 && args[0] != "-" && !maybeFile(args[0]) {
|
if expression == "" && len(args) > 0 && args[0] != "-" && !maybeFile(args[0]) {
|
||||||
yqlib.GetLogger().Debug("assuming expression is '%v'", args[0])
|
yqlib.GetLogger().Debug("assuming expression is '%v'", args[0])
|
||||||
expression = args[0]
|
expression = args[0]
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
return expression, args
|
return expression, args, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user