mirror of
https://github.com/mikefarah/yq.git
synced 2025-02-13 11:05:46 +00:00
Can merge one file
This commit is contained in:
parent
f1dbe13f21
commit
2fa8b24272
@ -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())
|
||||
}
|
||||
|
||||
|
30
cmd/merge.go
30
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})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user