Can merge one file

This commit is contained in:
Mike Farah 2020-02-04 14:33:35 +11:00
parent f1dbe13f21
commit 2fa8b24272
2 changed files with 33 additions and 15 deletions

View File

@ -1656,6 +1656,20 @@ c:
test.AssertResult(t, expectedOutput, result.Output) 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) { func TestMergeNoAutoCreateCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "merge -c=false ../examples/data1.yaml ../examples/data2.yaml") 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) { func TestMergeCmd_Error(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "merge ../examples/data1.yaml") result := test.RunCmd(cmd, "merge")
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to missing arg") 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()) test.AssertResult(t, expectedOutput, result.Error.Error())
} }

View File

@ -40,12 +40,15 @@ If append flag is set then existing arrays will be merged with the arrays from e
} }
func mergeProperties(cmd *cobra.Command, args []string) error { func mergeProperties(cmd *cobra.Command, args []string) error {
if len(args) < 2 { var updateCommands []yqlib.UpdateCommand = make([]yqlib.UpdateCommand, 0)
return errors.New("Must provide at least 2 yaml files")
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 // first generate update commands from the file
var filesToMerge = args[1:] var filesToMerge = args[1:]
var updateCommands []yqlib.UpdateCommand = make([]yqlib.UpdateCommand, 0)
for _, fileToMerge := range filesToMerge { for _, fileToMerge := range filesToMerge {
matchingNodes, errorProcessingFile := readYamlFile(fileToMerge, "**", false, 0) matchingNodes, errorProcessingFile := readYamlFile(fileToMerge, "**", false, 0)
@ -57,6 +60,7 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag}) updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag})
} }
} }
}
return updateDoc(args[0], updateCommands, cmd.OutOrStdout()) return updateDoc(args[0], updateCommands, cmd.OutOrStdout())
} }