mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
Merge arrays!
This commit is contained in:
parent
1aa5ec1d40
commit
1f7f1b0def
10
Upgrade Notes
Normal file
10
Upgrade Notes
Normal file
@ -0,0 +1,10 @@
|
||||
# New Features
|
||||
- Keeps comments and formatting (e.g. inline arrays)!
|
||||
- Handles anchors!
|
||||
- Can specify yaml tags (e.g. !!int)
|
||||
|
||||
# Update scripts file format has changed
|
||||
|
||||
# Merge command
|
||||
- autocreates missing entries in target by default, new flag to turn that off.
|
||||
|
@ -1342,34 +1342,26 @@ c:
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
||||
func xTestMergeAppendCmd(t *testing.T) {
|
||||
func TestMergeAppendCmd(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, "merge --append examples/data1.yaml examples/data2.yaml")
|
||||
result := test.RunCmd(cmd, "merge --autocreate=false --append examples/data1.yaml examples/data2.yaml")
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `a: simple
|
||||
b:
|
||||
- 1
|
||||
- 2
|
||||
- 3
|
||||
- 4
|
||||
b: [1, 2, 3, 4]
|
||||
c:
|
||||
test: 1
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
func xTestMergeArraysCmd(t *testing.T) {
|
||||
func TestMergeArraysCmd(t *testing.T) {
|
||||
cmd := getRootCommand()
|
||||
result := test.RunCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `- 1
|
||||
- 2
|
||||
- 3
|
||||
- 4
|
||||
- 5
|
||||
expectedOutput := `[1, 2, 3, 4, 5]
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
[4,5]
|
||||
- 4
|
||||
- 5
|
@ -34,11 +34,20 @@ func DebugNode(value *yaml.Node) {
|
||||
}
|
||||
|
||||
func PathStackToString(pathStack []interface{}) string {
|
||||
return MergePathStackToString(pathStack, false)
|
||||
}
|
||||
|
||||
func MergePathStackToString(pathStack []interface{}, appendArrays bool) string {
|
||||
var sb strings.Builder
|
||||
for index, path := range pathStack {
|
||||
switch path.(type) {
|
||||
case int:
|
||||
sb.WriteString(fmt.Sprintf("[%v]", path))
|
||||
if appendArrays {
|
||||
sb.WriteString("[+]")
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("[%v]", path))
|
||||
}
|
||||
|
||||
default:
|
||||
sb.WriteString(fmt.Sprintf("%v", path))
|
||||
}
|
||||
|
4
yq.go
4
yq.go
@ -238,7 +238,7 @@ Note that if you set both flags only overwrite will take effect.
|
||||
cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
||||
cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values")
|
||||
cmdMerge.PersistentFlags().BoolVarP(&autoCreateFlag, "autocreate", "c", true, "automatically create any missing entries")
|
||||
// cmdMerge.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "update the yaml file by appending array values")
|
||||
cmdMerge.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "update the yaml file by appending array values")
|
||||
// cmdMerge.PersistentFlags().BoolVarP(&allowEmptyFlag, "allow-empty", "e", false, "allow empty yaml files")
|
||||
cmdMerge.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||
return cmdMerge
|
||||
@ -435,7 +435,7 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
|
||||
return errorProcessingFile
|
||||
}
|
||||
for _, matchingNode := range matchingNodes {
|
||||
mergePath := yqlib.PathStackToString(matchingNode.PathStack)
|
||||
mergePath := yqlib.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