diff --git a/cmd/new.go b/cmd/new.go index ebe018a3..fede2523 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -2,7 +2,6 @@ package cmd import ( "github.com/mikefarah/yq/v3/pkg/yqlib" - errors "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,11 +35,7 @@ Note that you can give a create script to perform more sophisticated yaml. This func newProperty(cmd *cobra.Command, args []string) error { var badArgsMessage = "Must provide " - if len(args) != 2 { - return errors.New(badArgsMessage) - } - - var updateCommands, updateCommandsError = readUpdateCommands(args, 2, badArgsMessage) + var updateCommands, updateCommandsError = readUpdateCommands(args, 2, badArgsMessage, false) if updateCommandsError != nil { return updateCommandsError } diff --git a/cmd/new_test.go b/cmd/new_test.go index 4697e8da..bdd9f274 100644 --- a/cmd/new_test.go +++ b/cmd/new_test.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "testing" "github.com/mikefarah/yq/v3/test" @@ -18,6 +19,24 @@ func TestNewCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestNewCmdScript(t *testing.T) { + updateScript := `- command: update + path: b.c + value: 7` + scriptFilename := test.WriteTempYamlFile(updateScript) + defer test.RemoveTempYamlFile(scriptFilename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("new --script %s", scriptFilename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 7 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestNewAnchorCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "new b.c 3 --anchorName=fred") diff --git a/cmd/utils.go b/cmd/utils.go index 8696a30b..85f6c1d8 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -477,7 +477,7 @@ type updateCommandParsed struct { Value yaml.Node } -func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) ([]yqlib.UpdateCommand, error) { +func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string, allowNoValue bool) ([]yqlib.UpdateCommand, error) { var updateCommands []yqlib.UpdateCommand = make([]yqlib.UpdateCommand, 0) if writeScript != "" { var parsedCommands = make([]updateCommandParsed, 0) @@ -513,7 +513,7 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) log.Debug("Value %v", args[expectedArgs-1]) value := valueParser.Parse(args[expectedArgs-1], customTag, customStyle, anchorName, makeAlias) updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value, Overwrite: true, DontUpdateComments: true} - } else if len(args) == expectedArgs-1 { + } else if len(args) == expectedArgs-1 && allowNoValue { // don't update the value updateCommands = make([]yqlib.UpdateCommand, 1) log.Debug("args %v", args) diff --git a/cmd/write.go b/cmd/write.go index 052b48c5..be14a132 100644 --- a/cmd/write.go +++ b/cmd/write.go @@ -53,7 +53,7 @@ format is list of update commands (update or delete) like so: } func writeProperty(cmd *cobra.Command, args []string) error { - var updateCommands, updateCommandsError = readUpdateCommands(args, 3, "Must provide ") + var updateCommands, updateCommandsError = readUpdateCommands(args, 3, "Must provide ", true) if updateCommandsError != nil { return updateCommandsError }