From b3b60665e43575f7240cef9e6ced7902ba999607 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 26 Jun 2018 14:09:56 +1000 Subject: [PATCH] Fixed toJson command line option, should only apply to read command --- README.md | 1 - commands_test.go | 46 ++++++++++------------------------------------ yq.go | 12 ++++++------ 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index a638fbb9..57a03877 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,6 @@ Available Commands: Flags: -h, --help help for yq - -j, --tojson output as json -t, --trim trim yaml output (default true) -v, --verbose verbose mode -V, --version Print version information and quit diff --git a/commands_test.go b/commands_test.go index da57375f..9c2bfdfa 100644 --- a/commands_test.go +++ b/commands_test.go @@ -73,30 +73,6 @@ func TestRootCmd_TrimShort(t *testing.T) { } } -func TestRootCmd_ToJsonLong(t *testing.T) { - cmd := getRootCommand() - result := runCmd(cmd, "--tojson") - if result.Error != nil { - t.Error(result.Error) - } - - if !outputToJSON { - t.Error("Expected outputToJSON to be true") - } -} - -func TestRootCmd_ToJsonShort(t *testing.T) { - cmd := getRootCommand() - result := runCmd(cmd, "-j") - if result.Error != nil { - t.Error(result.Error) - } - - if !outputToJSON { - t.Error("Expected outputToJSON to be true") - } -} - func TestRootCmd_VersionShort(t *testing.T) { cmd := getRootCommand() result := runCmd(cmd, "-V") @@ -302,7 +278,16 @@ func TestReadCmd_NoTrim(t *testing.T) { func TestReadCmd_ToJson(t *testing.T) { cmd := getRootCommand() - result := runCmd(cmd, "-j read examples/sample.yaml b.c") + result := runCmd(cmd, "read -j examples/sample.yaml b.c") + if result.Error != nil { + t.Error(result.Error) + } + assertResult(t, "2\n", result.Output) +} + +func TestReadCmd_ToJsonLong(t *testing.T) { + cmd := getRootCommand() + result := runCmd(cmd, "read --tojson examples/sample.yaml b.c") if result.Error != nil { t.Error(result.Error) } @@ -343,17 +328,6 @@ func TestNewCmd_Verbose(t *testing.T) { assertResult(t, expectedOutput, result.Output) } -func TestNewCmd_ToJson(t *testing.T) { - cmd := getRootCommand() - result := runCmd(cmd, "-j new b.c 3") - if result.Error != nil { - t.Error(result.Error) - } - expectedOutput := `{"b":{"c":3}} -` - assertResult(t, expectedOutput, result.Output) -} - func TestWriteCmd(t *testing.T) { content := `b: c: 3 diff --git a/yq.go b/yq.go index 286e99f8..17b7be9a 100644 --- a/yq.go +++ b/yq.go @@ -66,7 +66,6 @@ func newCommandCLI() *cobra.Command { } rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") - rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit") @@ -86,7 +85,7 @@ func createReadCmd() *cobra.Command { var cmdRead = &cobra.Command{ Use: "read [yaml_file] [path]", Aliases: []string{"r"}, - Short: "yq r [--doc/-d document_index] sample.yaml a.b.c", + Short: "yq r [--doc/-d index] sample.yaml a.b.c", Example: ` yq read things.yaml a.b.c yq r - a.b.c (reads from stdin) @@ -97,7 +96,8 @@ yq r things.yaml a.array[*].blah Long: "Outputs the value of the given path in the yaml file to STDOUT", RunE: readProperty, } - cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based") + cmdRead.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number, 0 based") + cmdRead.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") return cmdRead } @@ -105,7 +105,7 @@ func createWriteCmd() *cobra.Command { var cmdWrite = &cobra.Command{ Use: "write [yaml_file] [path] [value]", Aliases: []string{"w"}, - Short: "yq w [--inplace/-i] [--script/-s script_file] [--doc/-d document_index] sample.yaml a.b.c newValueForC", + Short: "yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml a.b.c newValueForC", Example: ` yq write things.yaml a.b.c cat yq write --inplace things.yaml a.b.c cat @@ -140,7 +140,7 @@ func createDeleteCmd() *cobra.Command { var cmdDelete = &cobra.Command{ Use: "delete [yaml_file] [path]", Aliases: []string{"d"}, - Short: "yq d [--inplace/-i] [--doc/-d document_index] sample.yaml a.b.c", + Short: "yq d [--inplace/-i] [--doc/-d index] sample.yaml a.b.c", Example: ` yq delete things.yaml a.b.c yq delete --inplace things.yaml a.b.c @@ -183,7 +183,7 @@ func createMergeCmd() *cobra.Command { var cmdMerge = &cobra.Command{ Use: "merge [initial_yaml_file] [additional_yaml_file]...", Aliases: []string{"m"}, - Short: "yq m [--inplace/-i] [--doc/-d document_index] [--overwrite/-x] sample.yaml sample2.yaml", + Short: "yq m [--inplace/-i] [--doc/-d index] [--overwrite/-x] sample.yaml sample2.yaml", Example: ` yq merge things.yaml other.yaml yq merge --inplace things.yaml other.yaml