From c9229439f74282db466673a468642c27c6d57e44 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 30 Nov 2020 16:35:21 +1100 Subject: [PATCH] added exit status --- cmd/constant.go | 2 +- cmd/evaluate_all_command.go | 7 ++++++- cmd/evalute_sequence_command.go | 8 +++++++- cmd/root.go | 1 + pkg/yqlib/printer.go | 9 +++++++++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/constant.go b/cmd/constant.go index 98e20796..fb8f94d4 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -5,7 +5,7 @@ var unwrapScalar = true var writeInplace = false var outputToJSON = false -// var exitStatus = false +var exitStatus = false var forceColor = false var forceNoColor = false var colorsEnabled = false diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index ec4bf20a..5bf24654 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "os" @@ -24,6 +25,7 @@ yq es -n '{"a": "b"}' return cmdEvalAll } func evaluateAll(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence @@ -81,6 +83,9 @@ func evaluateAll(cmd *cobra.Command, args []string) error { completedSuccessfully = err == nil - cmd.SilenceUsage = true + if err == nil && exitStatus && !printer.PrintedAnything() { + return errors.New("no matches found") + } + return err } diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index cdd70bd0..45625207 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "os" @@ -24,6 +25,7 @@ yq es -n '{"a": "b"}' return cmdEvalSequence } func evaluateSequence(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence @@ -81,6 +83,10 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { err = streamEvaluator.EvaluateFiles(args[0], args[1:], printer) } completedSuccessfully = err == nil - cmd.SilenceUsage = true + + if err == nil && exitStatus && !printer.PrintedAnything() { + return errors.New("no matches found") + } + return err } diff --git a/cmd/root.go b/cmd/root.go index 05878555..9dbe6521 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,6 +47,7 @@ func New() *cobra.Command { rootCmd.PersistentFlags().IntVarP(&indent, "indent", "I", 2, "sets indent level for output") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit") rootCmd.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace of first yaml file given.") + rootCmd.PersistentFlags().BoolVarP(&exitStatus, "exit-status", "e", false, "set exit status if there are no matches or null or false is returned") rootCmd.PersistentFlags().BoolVarP(&forceColor, "colors", "C", false, "force print with colors") rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 6c7f41e7..1da0728a 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -10,6 +10,7 @@ import ( type Printer interface { PrintResults(matchingNodes *list.List) error + PrintedAnything() bool } type resultsPrinter struct { @@ -21,6 +22,7 @@ type resultsPrinter struct { writer io.Writer firstTimePrinting bool previousDocIndex uint + printedMatches bool } func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer { @@ -35,7 +37,14 @@ func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEn } } +func (p *resultsPrinter) PrintedAnything() bool { + return p.printedMatches +} + func (p *resultsPrinter) printNode(node *yaml.Node, writer io.Writer) error { + p.printedMatches = p.printedMatches || (node.Tag != "!!null" && + (node.Tag != "!!bool" || node.Value != "false")) + var encoder Encoder if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON { return p.writeString(writer, node.Value+"\n")