From d1c1ab0a7502afa30f04309aee85a346baf34295 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 30 Jan 2020 16:34:43 +1100 Subject: [PATCH] Added validate command --- cmd/commands_test.go | 25 ++++++++++++++++++++++++- cmd/root.go | 1 + cmd/validate.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 cmd/validate.go diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 1d96a28..19217ab 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -94,6 +94,15 @@ func TestReadCmd(t *testing.T) { test.AssertResult(t, "2", result.Output) } +func TestValidateCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "validate ../examples/sample.yaml b.c") + if result.Error != nil { + t.Error(result.Error) + } + test.AssertResult(t, "", result.Output) +} + func TestReadWithAdvancedFilterCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read -v ../examples/sample.yaml b.e(name==sam).value") @@ -547,7 +556,21 @@ func TestReadBadDataCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, fmt.Sprintf("read %s", filename)) if result.Error == nil { - t.Error("Expected command to fail due to invalid path") + t.Error("Expected command to fail") + } + expectedOutput := `yaml: line 1: did not find expected ',' or ']'` + test.AssertResult(t, expectedOutput, result.Error.Error()) +} + +func TestValidateBadDataCmd(t *testing.T) { + content := `[!Whatever]` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("validate %s", filename)) + if result.Error == nil { + t.Error("Expected command to fail") } expectedOutput := `yaml: line 1: did not find expected ',' or ']'` test.AssertResult(t, expectedOutput, result.Error.Error()) diff --git a/cmd/root.go b/cmd/root.go index 84e4703..f8eb8f6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,6 +44,7 @@ func New() *cobra.Command { rootCmd.AddCommand( createReadCmd(), + createValidateCmd(), createWriteCmd(), createPrefixCmd(), createDeleteCmd(), diff --git a/cmd/validate.go b/cmd/validate.go new file mode 100644 index 0000000..2527168 --- /dev/null +++ b/cmd/validate.go @@ -0,0 +1,37 @@ +package cmd + +import ( + errors "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +func createValidateCmd() *cobra.Command { + var cmdRead = &cobra.Command{ + Use: "validate [yaml_file]", + Aliases: []string{"v"}, + Short: "yq v sample.yaml", + Example: ` +yq v - # reads from stdin +`, + RunE: validateProperty, + SilenceUsage: true, + SilenceErrors: true, + } + cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)") + return cmdRead +} + +func validateProperty(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("Must provide filename") + } + + var updateAll, docIndexInt, errorParsingDocIndex = parseDocumentIndex() + if errorParsingDocIndex != nil { + return errorParsingDocIndex + } + + _, errorReadingStream := readYamlFile(args[0], "", updateAll, docIndexInt) + + return errorReadingStream +}