mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	added exit status
This commit is contained in:
		
							parent
							
								
									9bc66c80b6
								
							
						
					
					
						commit
						c9229439f7
					
				| @ -5,7 +5,7 @@ var unwrapScalar = true | |||||||
| var writeInplace = false | var writeInplace = false | ||||||
| var outputToJSON = false | var outputToJSON = false | ||||||
| 
 | 
 | ||||||
| // var exitStatus = false | var exitStatus = false | ||||||
| var forceColor = false | var forceColor = false | ||||||
| var forceNoColor = false | var forceNoColor = false | ||||||
| var colorsEnabled = false | var colorsEnabled = false | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package cmd | package cmd | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
| @ -24,6 +25,7 @@ yq es -n '{"a": "b"}' | |||||||
| 	return cmdEvalAll | 	return cmdEvalAll | ||||||
| } | } | ||||||
| func evaluateAll(cmd *cobra.Command, args []string) error { | func evaluateAll(cmd *cobra.Command, args []string) error { | ||||||
|  | 	cmd.SilenceUsage = true | ||||||
| 	// 0 args, read std in | 	// 0 args, read std in | ||||||
| 	// 1 arg, null input, process expression | 	// 1 arg, null input, process expression | ||||||
| 	// 1 arg, read file in sequence | 	// 1 arg, read file in sequence | ||||||
| @ -81,6 +83,9 @@ func evaluateAll(cmd *cobra.Command, args []string) error { | |||||||
| 
 | 
 | ||||||
| 	completedSuccessfully = err == nil | 	completedSuccessfully = err == nil | ||||||
| 
 | 
 | ||||||
| 	cmd.SilenceUsage = true | 	if err == nil && exitStatus && !printer.PrintedAnything() { | ||||||
|  | 		return errors.New("no matches found") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package cmd | package cmd | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
| @ -24,6 +25,7 @@ yq es -n '{"a": "b"}' | |||||||
| 	return cmdEvalSequence | 	return cmdEvalSequence | ||||||
| } | } | ||||||
| func evaluateSequence(cmd *cobra.Command, args []string) error { | func evaluateSequence(cmd *cobra.Command, args []string) error { | ||||||
|  | 	cmd.SilenceUsage = true | ||||||
| 	// 0 args, read std in | 	// 0 args, read std in | ||||||
| 	// 1 arg, null input, process expression | 	// 1 arg, null input, process expression | ||||||
| 	// 1 arg, read file in sequence | 	// 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) | 		err = streamEvaluator.EvaluateFiles(args[0], args[1:], printer) | ||||||
| 	} | 	} | ||||||
| 	completedSuccessfully = err == nil | 	completedSuccessfully = err == nil | ||||||
| 	cmd.SilenceUsage = true | 
 | ||||||
|  | 	if err == nil && exitStatus && !printer.PrintedAnything() { | ||||||
|  | 		return errors.New("no matches found") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  | |||||||
| @ -47,6 +47,7 @@ func New() *cobra.Command { | |||||||
| 	rootCmd.PersistentFlags().IntVarP(&indent, "indent", "I", 2, "sets indent level for output") | 	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.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(&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(&forceColor, "colors", "C", false, "force print with colors") | ||||||
| 	rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") | 	rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") | ||||||
|  | |||||||
| @ -10,6 +10,7 @@ import ( | |||||||
| 
 | 
 | ||||||
| type Printer interface { | type Printer interface { | ||||||
| 	PrintResults(matchingNodes *list.List) error | 	PrintResults(matchingNodes *list.List) error | ||||||
|  | 	PrintedAnything() bool | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type resultsPrinter struct { | type resultsPrinter struct { | ||||||
| @ -21,6 +22,7 @@ type resultsPrinter struct { | |||||||
| 	writer             io.Writer | 	writer             io.Writer | ||||||
| 	firstTimePrinting  bool | 	firstTimePrinting  bool | ||||||
| 	previousDocIndex   uint | 	previousDocIndex   uint | ||||||
|  | 	printedMatches     bool | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer { | 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 { | 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 | 	var encoder Encoder | ||||||
| 	if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON { | 	if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON { | ||||||
| 		return p.writeString(writer, node.Value+"\n") | 		return p.writeString(writer, node.Value+"\n") | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user