mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22:38:04 +00:00
Added exit flag
This commit is contained in:
parent
9624410add
commit
d473c39a44
@ -16,6 +16,7 @@ var writeInplace = false
|
||||
var writeScript = ""
|
||||
var sourceYamlFile = ""
|
||||
var outputToJSON = false
|
||||
var exitStatus = false
|
||||
var prettyPrint = false
|
||||
var explodeAnchors = 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(&stripComments, "stripComments", "", false, "print yaml without any comments")
|
||||
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
|
||||
}
|
||||
|
||||
@ -50,6 +51,11 @@ func readProperty(cmd *cobra.Command, args []string) error {
|
||||
|
||||
matchingNodes, errorReadingStream := readYamlFile(args[0], path, updateAll, docIndexInt)
|
||||
|
||||
if exitStatus {
|
||||
cmd.SilenceUsage = true
|
||||
return errors.New("No matches found")
|
||||
}
|
||||
|
||||
if errorReadingStream != nil {
|
||||
return errorReadingStream
|
||||
}
|
||||
|
@ -882,6 +882,24 @@ b:
|
||||
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) {
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, "read -P -I4 ../examples/sample.json")
|
||||
|
Loading…
Reference in New Issue
Block a user