diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 62231e44..266fb2c6 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -1656,6 +1656,20 @@ c: test.AssertResult(t, expectedOutput, result.Output) } +func TestMergeOneFileCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "merge ../examples/data1.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `a: simple # just the best +b: [1, 2] +c: + test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestMergeNoAutoCreateCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "merge -c=false ../examples/data1.yaml ../examples/data2.yaml") @@ -1808,11 +1822,11 @@ apples: red func TestMergeCmd_Error(t *testing.T) { cmd := getRootCommand() - result := test.RunCmd(cmd, "merge ../examples/data1.yaml") + result := test.RunCmd(cmd, "merge") if result.Error == nil { t.Error("Expected command to fail due to missing arg") } - expectedOutput := `Must provide at least 2 yaml files` + expectedOutput := `Must provide at least 1 yaml file` test.AssertResult(t, expectedOutput, result.Error.Error()) } diff --git a/cmd/merge.go b/cmd/merge.go index 084b6ebb..0d9905ae 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -40,21 +40,25 @@ If append flag is set then existing arrays will be merged with the arrays from e } func mergeProperties(cmd *cobra.Command, args []string) error { - if len(args) < 2 { - return errors.New("Must provide at least 2 yaml files") - } - // first generate update commands from the file - var filesToMerge = args[1:] var updateCommands []yqlib.UpdateCommand = make([]yqlib.UpdateCommand, 0) - for _, fileToMerge := range filesToMerge { - matchingNodes, errorProcessingFile := readYamlFile(fileToMerge, "**", false, 0) - if errorProcessingFile != nil && (!allowEmptyFlag || !strings.HasPrefix(errorProcessingFile.Error(), "Could not process document index")) { - return errorProcessingFile - } - for _, matchingNode := range matchingNodes { - mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag) - updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag}) + if len(args) < 1 { + return errors.New("Must provide at least 1 yaml file") + } + + if len(args) > 1 { + // first generate update commands from the file + var filesToMerge = args[1:] + + for _, fileToMerge := range filesToMerge { + matchingNodes, errorProcessingFile := readYamlFile(fileToMerge, "**", false, 0) + if errorProcessingFile != nil && (!allowEmptyFlag || !strings.HasPrefix(errorProcessingFile.Error(), "Could not process document index")) { + return errorProcessingFile + } + for _, matchingNode := range matchingNodes { + mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag) + updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag}) + } } }