Fixed toJson command line option, should only apply to read command

This commit is contained in:
Mike Farah 2018-06-26 14:09:56 +10:00
parent df08b055cf
commit b3b60665e4
3 changed files with 16 additions and 43 deletions

View File

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

View File

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

12
yq.go
View File

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