2020-11-13 02:19:54 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-30 05:35:21 +00:00
|
|
|
"errors"
|
2020-11-13 02:19:54 +00:00
|
|
|
|
|
|
|
"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",
|
2024-11-16 00:52:42 +00:00
|
|
|
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
|
2023-12-20 07:36:57 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
|
|
},
|
2020-11-13 02:19:54 +00:00
|
|
|
Example: `
|
2023-09-18 23:52:36 +00:00
|
|
|
# Merge f2.yml into f1.yml (in place)
|
2021-07-09 04:58:09 +00:00
|
|
|
yq eval-all --inplace 'select(fileIndex == 0) * select(fileIndex == 1)' f1.yml f2.yml
|
|
|
|
## the same command and expression using shortened names:
|
|
|
|
yq ea -i 'select(fi == 0) * select(fi == 1)' f1.yml f2.yml
|
2021-04-19 00:26:58 +00:00
|
|
|
|
2021-07-09 04:58:09 +00:00
|
|
|
|
|
|
|
# Merge all given files
|
|
|
|
yq ea '. as $item ireduce ({}; . * $item )' file1.yml file2.yml ...
|
|
|
|
|
2021-09-19 23:57:41 +00:00
|
|
|
# Pipe from STDIN
|
|
|
|
## use '-' as a filename to pipe from STDIN
|
2021-04-19 00:26:58 +00:00
|
|
|
cat file2.yml | yq ea '.a.b' file1.yml - file3.yml
|
2020-11-13 02:19:54 +00:00
|
|
|
`,
|
2023-11-30 04:23:05 +00:00
|
|
|
Long: `yq is a portable command-line data file processor (https://github.com/mikefarah/yq/)
|
2021-07-09 04:58:09 +00:00
|
|
|
See https://mikefarah.gitbook.io/yq/ for detailed documentation and examples.
|
|
|
|
|
|
|
|
## Evaluate All ##
|
|
|
|
This command loads _all_ yaml documents of _all_ yaml files and runs expression once
|
|
|
|
Useful when you need to run an expression across several yaml documents or files (like merge).
|
|
|
|
Note that it consumes more memory than eval.
|
|
|
|
`,
|
2020-11-13 02:19:54 +00:00
|
|
|
RunE: evaluateAll,
|
|
|
|
}
|
|
|
|
return cmdEvalAll
|
|
|
|
}
|
2022-01-14 04:40:20 +00:00
|
|
|
func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
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
|
|
|
|
|
2022-04-26 23:08:50 +00:00
|
|
|
expression, args, err := initCommand(cmd, args)
|
2021-10-30 02:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-10 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 02:04:05 +00:00
|
|
|
out := cmd.OutOrStdout()
|
2021-10-29 03:14:39 +00:00
|
|
|
|
2020-11-29 09:25:47 +00:00
|
|
|
if writeInplace {
|
2020-11-30 05:05:07 +00:00
|
|
|
// only use colors if its forced
|
|
|
|
colorsEnabled = forceColor
|
2022-04-26 23:08:50 +00:00
|
|
|
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[0])
|
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
|
2022-01-14 04:40:20 +00:00
|
|
|
defer func() {
|
|
|
|
if cmdError == nil {
|
|
|
|
cmdError = writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully)
|
|
|
|
}
|
|
|
|
}()
|
2020-11-29 09:25:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 05:37:13 +00:00
|
|
|
format, err := yqlib.FormatFromString(outputFormat)
|
2021-07-25 08:08:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-09 01:23:06 +00:00
|
|
|
|
2023-03-15 02:22:58 +00:00
|
|
|
decoder, err := configureDecoder(true)
|
2021-12-21 04:02:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:29:41 +00:00
|
|
|
printerWriter, err := configurePrinterWriter(format, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-01 02:19:06 +00:00
|
|
|
encoder, err := configureEncoder()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-29 03:14:39 +00:00
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
printer := yqlib.NewPrinter(encoder, printerWriter)
|
2023-03-28 22:51:55 +00:00
|
|
|
if nulSepOutput {
|
|
|
|
printer.SetNulSepOutput(true)
|
|
|
|
}
|
2020-11-29 09:25:47 +00:00
|
|
|
|
2021-07-18 03:17:35 +00:00
|
|
|
if frontMatter != "" {
|
2022-04-26 23:08:50 +00:00
|
|
|
frontMatterHandler := yqlib.NewFrontMatterHandler(args[0])
|
2021-07-18 03:17:35 +00:00
|
|
|
err = frontMatterHandler.Split()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-26 23:08:50 +00:00
|
|
|
args[0] = frontMatterHandler.GetYamlFrontMatterFilename()
|
2021-07-18 03:17:35 +00:00
|
|
|
|
|
|
|
if frontMatter == "process" {
|
2021-07-20 00:38:42 +00:00
|
|
|
reader := frontMatterHandler.GetContentReader()
|
2021-07-18 03:17:35 +00:00
|
|
|
printer.SetAppendix(reader)
|
|
|
|
defer yqlib.SafelyCloseReader(reader)
|
|
|
|
}
|
|
|
|
defer frontMatterHandler.CleanUp()
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
2022-02-06 21:46:43 +00:00
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
if nullInput {
|
2022-10-28 03:16:46 +00:00
|
|
|
err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(expression), printer)
|
2020-11-13 02:19:54 +00:00
|
|
|
} else {
|
2022-02-06 22:27:52 +00:00
|
|
|
cmd.Println(cmd.UsageString())
|
|
|
|
return nil
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
default:
|
2022-10-28 03:16:46 +00:00
|
|
|
err = allAtOnceEvaluator.EvaluateFiles(processExpression(expression), args, printer, decoder)
|
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
|
|
|
|
}
|