diff --git a/cmd/new.go b/cmd/new.go index 70d99935..0a26da1b 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/mikefarah/yq/v3/pkg/yqlib" + errors "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -32,7 +33,12 @@ Note that you can give a create script to perform more sophisticated yaml. This } func newProperty(cmd *cobra.Command, args []string) error { - var updateCommands, updateCommandsError = readUpdateCommands(args, 2, "Must provide ") + var badArgsMessage = "Must provide " + if len(args) != 2 { + return errors.New(badArgsMessage) + } + + var updateCommands, updateCommandsError = readUpdateCommands(args, 2, badArgsMessage) if updateCommandsError != nil { return updateCommandsError } diff --git a/cmd/utils.go b/cmd/utils.go index 6af24fa7..d75f55cb 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -430,15 +430,20 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) log.Debug("args %v", args[expectedArgs-2]) updateCommands = make([]yqlib.UpdateCommand, 1) updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value.Content[0], Overwrite: true} - - } else if len(args) < expectedArgs { - return nil, errors.New(badArgsMessage) - } else { + } else if len(args) == expectedArgs { updateCommands = make([]yqlib.UpdateCommand, 1) log.Debug("args %v", args) log.Debug("path %v", args[expectedArgs-2]) log.Debug("Value %v", args[expectedArgs-1]) updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: valueParser.Parse(args[expectedArgs-1], customTag, customStyle), Overwrite: true} + } else if len(args) == expectedArgs-1 { + // don't update the value + updateCommands = make([]yqlib.UpdateCommand, 1) + log.Debug("args %v", args) + log.Debug("path %v", args[expectedArgs-2]) + updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: valueParser.Parse("", customTag, customStyle), Overwrite: true, DontUpdateNodeValue: true} + } else { + return nil, errors.New(badArgsMessage) } return updateCommands, nil } diff --git a/cmd/write_test.go b/cmd/write_test.go index db9c4a25..7ed192a3 100644 --- a/cmd/write_test.go +++ b/cmd/write_test.go @@ -63,6 +63,46 @@ func TestWriteWithDoubleQuotedStyleCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestWriteUpdateStyleOnlyCmd(t *testing.T) { + content := `b: + c: dog + d: things +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("write %s b.* --style=single", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 'dog' + d: 'things' +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestWriteUpdateTagOnlyCmd(t *testing.T) { + content := `b: + c: true + d: false +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("write %s b.* --tag=!!str", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: "true" + d: "false" +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestWriteWithSingleQuotedStyleCmd(t *testing.T) { content := `b: c: dog diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 89036291..8a1c8093 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -13,10 +13,11 @@ import ( var log = logging.MustGetLogger("yq") type UpdateCommand struct { - Command string - Path string - Value *yaml.Node - Overwrite bool + Command string + Path string + Value *yaml.Node + Overwrite bool + DontUpdateNodeValue bool } func KindString(kind yaml.Kind) string { diff --git a/pkg/yqlib/update_navigation_strategy.go b/pkg/yqlib/update_navigation_strategy.go index 11337422..557ead59 100644 --- a/pkg/yqlib/update_navigation_strategy.go +++ b/pkg/yqlib/update_navigation_strategy.go @@ -21,7 +21,9 @@ func UpdateNavigationStrategy(updateCommand UpdateCommand, autoCreate bool) Navi DebugNode(node) log.Debug("with") DebugNode(changesToApply) - node.Value = changesToApply.Value + if !updateCommand.DontUpdateNodeValue { + node.Value = changesToApply.Value + } node.Tag = changesToApply.Tag node.Kind = changesToApply.Kind node.Style = changesToApply.Style