2020-11-13 02:19:54 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-30 05:35:21 +00:00
|
|
|
"errors"
|
2020-11-29 09:25:47 +00:00
|
|
|
"fmt"
|
2020-11-13 02:19:54 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/pkg/yqlib"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createEvaluateAllCommand() *cobra.Command {
|
|
|
|
var cmdEvalAll = &cobra.Command{
|
|
|
|
Use: "eval-all [expression] [yaml_file1]...",
|
|
|
|
Aliases: []string{"ea"},
|
2020-11-13 03:07:11 +00:00
|
|
|
Short: "Loads _all_ yaml documents of _all_ yaml files and runs expression once",
|
2020-11-13 02:19:54 +00:00
|
|
|
Example: `
|
2020-12-01 05:16:20 +00:00
|
|
|
# merges f2.yml into f1.yml (inplace)
|
|
|
|
yq eval-all --inplace 'select(fileIndex == 0) * select(fileIndex == 1)' f1.yml f2.yml
|
2021-04-19 00:26:58 +00:00
|
|
|
|
|
|
|
# use '-' as a filename to read from STDIN
|
|
|
|
cat file2.yml | yq ea '.a.b' file1.yml - file3.yml
|
2020-11-13 02:19:54 +00:00
|
|
|
`,
|
2020-11-13 03:07:11 +00:00
|
|
|
Long: "Evaluate All:\nUseful when you need to run an expression across several yaml documents or files. Consumes more memory than eval",
|
2020-11-13 02:19:54 +00:00
|
|
|
RunE: evaluateAll,
|
|
|
|
}
|
|
|
|
return cmdEvalAll
|
|
|
|
}
|
|
|
|
func evaluateAll(cmd *cobra.Command, args []string) error {
|
2020-11-30 05:35:21 +00:00
|
|
|
cmd.SilenceUsage = true
|
2020-11-13 02:19:54 +00:00
|
|
|
// 0 args, read std in
|
|
|
|
// 1 arg, null input, process expression
|
|
|
|
// 1 arg, read file in sequence
|
|
|
|
// 2+ args, [0] = expression, file the rest
|
|
|
|
|
|
|
|
var err error
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
pipingStdIn := (stat.Mode() & os.ModeCharDevice) == 0
|
|
|
|
|
|
|
|
out := cmd.OutOrStdout()
|
|
|
|
|
|
|
|
fileInfo, _ := os.Stdout.Stat()
|
|
|
|
|
|
|
|
if forceColor || (!forceNoColor && (fileInfo.Mode()&os.ModeCharDevice) != 0) {
|
|
|
|
colorsEnabled = true
|
|
|
|
}
|
2020-11-29 09:25:47 +00:00
|
|
|
|
2021-02-10 06:06:16 +00:00
|
|
|
firstFileIndex := -1
|
|
|
|
if !nullInput && len(args) == 1 {
|
|
|
|
firstFileIndex = 0
|
|
|
|
} else if len(args) > 1 {
|
|
|
|
firstFileIndex = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeInplace && (firstFileIndex == -1) {
|
2020-11-29 09:25:47 +00:00
|
|
|
return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeInplace {
|
2020-11-30 05:05:07 +00:00
|
|
|
// only use colors if its forced
|
|
|
|
colorsEnabled = forceColor
|
2021-02-10 06:06:16 +00:00
|
|
|
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex])
|
2020-11-29 09:25:47 +00:00
|
|
|
out, err = writeInPlaceHandler.CreateTempFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-30 05:05:07 +00:00
|
|
|
// need to indirectly call the function so that completedSuccessfully is
|
|
|
|
// passed when we finish execution as opposed to now
|
|
|
|
defer func() { writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) }()
|
2020-11-29 09:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 01:23:06 +00:00
|
|
|
if nullInput && len(args) > 1 {
|
|
|
|
return errors.New("Cannot pass files in when using null-input flag")
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:35:59 +00:00
|
|
|
printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
|
2020-11-29 09:25:47 +00:00
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
2020-11-13 02:19:54 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
if pipingStdIn {
|
2020-12-28 00:40:41 +00:00
|
|
|
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer)
|
2020-11-13 02:19:54 +00:00
|
|
|
} else {
|
|
|
|
cmd.Println(cmd.UsageString())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
if nullInput {
|
2020-12-28 00:40:41 +00:00
|
|
|
err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer)
|
2020-11-13 02:19:54 +00:00
|
|
|
} else {
|
2020-12-28 00:40:41 +00:00
|
|
|
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer)
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-12-28 00:40:41 +00:00
|
|
|
err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer)
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 09:25:47 +00:00
|
|
|
completedSuccessfully = err == nil
|
|
|
|
|
2020-11-30 05:35:21 +00:00
|
|
|
if err == nil && exitStatus && !printer.PrintedAnything() {
|
|
|
|
return errors.New("no matches found")
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
return err
|
|
|
|
}
|