mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	Added exit flag
This commit is contained in:
		
							parent
							
								
									9624410add
								
							
						
					
					
						commit
						d473c39a44
					
				| @ -16,6 +16,7 @@ var writeInplace = false | |||||||
| var writeScript = "" | var writeScript = "" | ||||||
| var sourceYamlFile = "" | var sourceYamlFile = "" | ||||||
| var outputToJSON = false | var outputToJSON = false | ||||||
|  | var exitStatus = false | ||||||
| var prettyPrint = false | var prettyPrint = false | ||||||
| var explodeAnchors = false | var explodeAnchors = false | ||||||
| var colorsEnabled = false | var colorsEnabled = false | ||||||
|  | |||||||
| @ -31,6 +31,7 @@ yq r -- things.yaml '--key-starting-with-dashes.blah' | |||||||
| 	cmdRead.PersistentFlags().BoolVarP(&unwrapScalar, "unwrapScalar", "", true, "unwrap scalar, print the value with no quotes, colors or comments") | 	cmdRead.PersistentFlags().BoolVarP(&unwrapScalar, "unwrapScalar", "", true, "unwrap scalar, print the value with no quotes, colors or comments") | ||||||
| 	cmdRead.PersistentFlags().BoolVarP(&stripComments, "stripComments", "", false, "print yaml without any comments") | 	cmdRead.PersistentFlags().BoolVarP(&stripComments, "stripComments", "", false, "print yaml without any comments") | ||||||
| 	cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors") | 	cmdRead.PersistentFlags().BoolVarP(&explodeAnchors, "explodeAnchors", "X", false, "explode anchors") | ||||||
|  | 	cmdRead.PersistentFlags().BoolVarP(&exitStatus, "exitStatus", "e", false, "set exit status if no matches are found") | ||||||
| 	return cmdRead | 	return cmdRead | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -50,6 +51,11 @@ func readProperty(cmd *cobra.Command, args []string) error { | |||||||
| 
 | 
 | ||||||
| 	matchingNodes, errorReadingStream := readYamlFile(args[0], path, updateAll, docIndexInt) | 	matchingNodes, errorReadingStream := readYamlFile(args[0], path, updateAll, docIndexInt) | ||||||
| 
 | 
 | ||||||
|  | 	if exitStatus { | ||||||
|  | 		cmd.SilenceUsage = true | ||||||
|  | 		return errors.New("No matches found") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if errorReadingStream != nil { | 	if errorReadingStream != nil { | ||||||
| 		return errorReadingStream | 		return errorReadingStream | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -882,6 +882,24 @@ b: | |||||||
| 	test.AssertResult(t, expectedOutput, result.Output) | 	test.AssertResult(t, expectedOutput, result.Output) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func TestReadNotFoundWithExitStatus(t *testing.T) { | ||||||
|  | 	cmd := getRootCommand() | ||||||
|  | 	result := test.RunCmd(cmd, "read ../examples/sample.yaml adsf -e") | ||||||
|  | 	if result.Error == nil { | ||||||
|  | 		t.Error("Expected command to fail") | ||||||
|  | 	} | ||||||
|  | 	expectedOutput := `No matches found` | ||||||
|  | 	test.AssertResult(t, expectedOutput, result.Error.Error()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestReadNotFoundWithoutExitStatus(t *testing.T) { | ||||||
|  | 	cmd := getRootCommand() | ||||||
|  | 	result := test.RunCmd(cmd, "read ../examples/sample.yaml adsf") | ||||||
|  | 	if result.Error != nil { | ||||||
|  | 		t.Error("Expected command to succeed!") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func TestReadPrettyPrintWithIndentCmd(t *testing.T) { | func TestReadPrettyPrintWithIndentCmd(t *testing.T) { | ||||||
| 	cmd := getRootCommand() | 	cmd := getRootCommand() | ||||||
| 	result := test.RunCmd(cmd, "read -P -I4 ../examples/sample.json") | 	result := test.RunCmd(cmd, "read -P -I4 ../examples/sample.json") | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								yq.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								yq.go
									
									
									
									
									
								
							| @ -4,14 +4,11 @@ import ( | |||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
| 	command "github.com/mikefarah/yq/v3/cmd" | 	command "github.com/mikefarah/yq/v3/cmd" | ||||||
| 	logging "gopkg.in/op/go-logging.v1" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	cmd := command.New() | 	cmd := command.New() | ||||||
| 	log := logging.MustGetLogger("yq") |  | ||||||
| 	if err := cmd.Execute(); err != nil { | 	if err := cmd.Execute(); err != nil { | ||||||
| 		log.Error(err.Error()) |  | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user