diff --git a/Upgrade Notes b/Upgrade Notes new file mode 100644 index 00000000..153ebf50 --- /dev/null +++ b/Upgrade Notes @@ -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. + diff --git a/commands_test.go b/commands_test.go index 4f73533a..91b9cffe 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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) } diff --git a/examples/sample_array_2.yaml b/examples/sample_array_2.yaml index b32037f9..e83ee7a1 100644 --- a/examples/sample_array_2.yaml +++ b/examples/sample_array_2.yaml @@ -1 +1,2 @@ -[4,5] \ No newline at end of file +- 4 +- 5 \ No newline at end of file diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index dbf19183..18689815 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -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)) } diff --git a/yq.go b/yq.go index 22cace4c..a1333791 100644 --- a/yq.go +++ b/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}) } }