2020-11-06 03:37:01 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-30 05:35:21 +00:00
|
|
|
"errors"
|
2020-11-30 05:05:07 +00:00
|
|
|
"fmt"
|
2020-11-06 03:37:01 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/pkg/yqlib"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createEvaluateSequenceCommand() *cobra.Command {
|
|
|
|
var cmdEvalSequence = &cobra.Command{
|
2020-11-13 03:07:11 +00:00
|
|
|
Use: "eval [expression] [yaml_file1]...",
|
|
|
|
Aliases: []string{"e"},
|
2022-01-27 01:07:41 +00:00
|
|
|
Short: "(default) Apply the expression to each document in each yaml file in sequence",
|
2020-11-06 03:37:01 +00:00
|
|
|
Example: `
|
2021-07-09 04:58:09 +00:00
|
|
|
# Reads field under the given path for each file
|
|
|
|
yq e '.a.b' f1.yml f2.yml
|
2020-12-01 05:16:20 +00:00
|
|
|
|
2021-07-09 04:58:09 +00:00
|
|
|
# Prints out the file
|
2020-12-01 05:16:20 +00:00
|
|
|
yq e sample.yaml
|
|
|
|
|
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 e '.a.b' file1.yml - file3.yml
|
|
|
|
|
2021-07-09 04:58:09 +00:00
|
|
|
# Creates a new yaml document
|
|
|
|
## Note that editing an empty file does not work.
|
2020-12-01 05:16:20 +00:00
|
|
|
yq e -n '.a.b.c = "cat"'
|
|
|
|
|
2021-07-09 04:58:09 +00:00
|
|
|
# Update a file inplace
|
2020-12-01 05:16:20 +00:00
|
|
|
yq e '.a.b = "cool"' -i file.yaml
|
2020-11-06 03:37:01 +00:00
|
|
|
`,
|
2021-07-09 04:58:09 +00:00
|
|
|
Long: `yq is a portable command-line YAML processor (https://github.com/mikefarah/yq/)
|
|
|
|
See https://mikefarah.gitbook.io/yq/ for detailed documentation and examples.
|
|
|
|
|
|
|
|
## Evaluate Sequence ##
|
|
|
|
This command iterates over each yaml document from each given file, applies the
|
|
|
|
expression and prints the result in sequence.`,
|
2020-11-06 03:37:01 +00:00
|
|
|
RunE: evaluateSequence,
|
|
|
|
}
|
|
|
|
return cmdEvalSequence
|
|
|
|
}
|
2020-12-28 00:40:41 +00:00
|
|
|
|
|
|
|
func processExpression(expression string) string {
|
2022-01-21 09:26:09 +00:00
|
|
|
|
2020-12-28 00:40:41 +00:00
|
|
|
if prettyPrint && expression == "" {
|
2022-01-21 09:26:09 +00:00
|
|
|
return yqlib.PrettyPrintExp
|
2020-12-28 00:40:41 +00:00
|
|
|
} else if prettyPrint {
|
2022-01-21 09:26:09 +00:00
|
|
|
return fmt.Sprintf("%v | %v", expression, yqlib.PrettyPrintExp)
|
2020-12-28 00:40:41 +00:00
|
|
|
}
|
|
|
|
return expression
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:40:20 +00:00
|
|
|
func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
2020-11-06 03:37:01 +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
|
2021-10-30 02:04:05 +00:00
|
|
|
firstFileIndex, err := initCommand(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:37:01 +00:00
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
pipingStdIn := (stat.Mode() & os.ModeCharDevice) == 0
|
2022-01-27 02:24:05 +00:00
|
|
|
yqlib.GetLogger().Debug("pipingStdIn: %v", pipingStdIn)
|
2022-01-27 02:54:29 +00:00
|
|
|
|
|
|
|
yqlib.GetLogger().Debug("stat.Mode(): %v", stat.Mode())
|
2022-01-27 02:24:05 +00:00
|
|
|
yqlib.GetLogger().Debug("ModeDir: %v", stat.Mode()&os.ModeDir)
|
|
|
|
yqlib.GetLogger().Debug("ModeAppend: %v", stat.Mode()&os.ModeAppend)
|
|
|
|
yqlib.GetLogger().Debug("ModeExclusive: %v", stat.Mode()&os.ModeExclusive)
|
|
|
|
yqlib.GetLogger().Debug("ModeTemporary: %v", stat.Mode()&os.ModeTemporary)
|
|
|
|
yqlib.GetLogger().Debug("ModeSymlink: %v", stat.Mode()&os.ModeSymlink)
|
|
|
|
yqlib.GetLogger().Debug("ModeDevice: %v", stat.Mode()&os.ModeDevice)
|
|
|
|
yqlib.GetLogger().Debug("ModeNamedPipe: %v", stat.Mode()&os.ModeNamedPipe)
|
|
|
|
yqlib.GetLogger().Debug("ModeSocket: %v", stat.Mode()&os.ModeSocket)
|
|
|
|
yqlib.GetLogger().Debug("ModeSetuid: %v", stat.Mode()&os.ModeSetuid)
|
|
|
|
yqlib.GetLogger().Debug("ModeSetgid: %v", stat.Mode()&os.ModeSetgid)
|
|
|
|
yqlib.GetLogger().Debug("ModeCharDevice: %v", stat.Mode()&os.ModeCharDevice)
|
|
|
|
yqlib.GetLogger().Debug("ModeSticky: %v", stat.Mode()&os.ModeSticky)
|
|
|
|
yqlib.GetLogger().Debug("ModeIrregular: %v", stat.Mode()&os.ModeIrregular)
|
|
|
|
|
|
|
|
// Mask for the type bits. For regular files, none will be set.
|
|
|
|
yqlib.GetLogger().Debug("ModeType: %v", stat.Mode()&os.ModeType)
|
|
|
|
|
|
|
|
yqlib.GetLogger().Debug("ModePerm: %v", stat.Mode()&os.ModePerm)
|
2020-11-06 03:37:01 +00:00
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
out := cmd.OutOrStdout()
|
|
|
|
|
2020-11-30 05:05:07 +00:00
|
|
|
if writeInplace {
|
|
|
|
// only use colors if its forced
|
|
|
|
colorsEnabled = forceColor
|
2021-02-10 06:06:16 +00:00
|
|
|
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex])
|
2020-11-30 05:05:07 +00:00
|
|
|
out, err = writeInPlaceHandler.CreateTempFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 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-30 05:05:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 08:08:33 +00:00
|
|
|
format, err := yqlib.OutputFormatFromString(outputFormat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:29:41 +00:00
|
|
|
printerWriter, err := configurePrinterWriter(format, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder := configureEncoder(format)
|
2021-10-29 03:14:39 +00:00
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
printer := yqlib.NewPrinter(encoder, printerWriter)
|
2020-11-13 02:19:54 +00:00
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
decoder, err := configureDecoder()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
streamEvaluator := yqlib.NewStreamEvaluator()
|
|
|
|
|
2021-07-18 02:28:46 +00:00
|
|
|
if frontMatter != "" {
|
|
|
|
frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex])
|
|
|
|
err = frontMatterHandler.Split()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename()
|
|
|
|
|
|
|
|
if frontMatter == "process" {
|
2021-07-20 00:38:42 +00:00
|
|
|
reader := frontMatterHandler.GetContentReader()
|
2021-07-18 02:28:46 +00:00
|
|
|
printer.SetAppendix(reader)
|
|
|
|
defer yqlib.SafelyCloseReader(reader)
|
|
|
|
}
|
|
|
|
defer frontMatterHandler.CleanUp()
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:37:01 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
if pipingStdIn {
|
2021-12-21 04:02:07 +00:00
|
|
|
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
2020-11-06 03:37:01 +00:00
|
|
|
} else {
|
|
|
|
cmd.Println(cmd.UsageString())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
if nullInput {
|
2021-07-19 09:52:51 +00:00
|
|
|
err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer, "")
|
2022-01-26 23:18:46 +00:00
|
|
|
} else if pipingStdIn && args[0] != "-" {
|
|
|
|
// must have given a single expression and piping input from stdin
|
|
|
|
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
2020-11-06 03:37:01 +00:00
|
|
|
} else {
|
2021-12-21 04:02:07 +00:00
|
|
|
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
|
2020-11-06 03:37:01 +00:00
|
|
|
}
|
2020-11-13 02:19:54 +00:00
|
|
|
default:
|
2021-12-21 04:02:07 +00:00
|
|
|
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer, leadingContentPreProcessing, decoder)
|
2020-11-06 03:37:01 +00:00
|
|
|
}
|
2020-11-30 05:05:07 +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
|
2020-11-06 03:37:01 +00:00
|
|
|
}
|