1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Added exit flag

This commit is contained in:
Mike Farah
2020-06-10 16:54:08 +10:00
parent 9624410add
commit d473c39a44
4 changed files with 25 additions and 3 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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")