From 030df4cbe7ca2646937623dbbab0bfb9db54b2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renzo=20Cris=C3=B3stomo?= Date: Wed, 20 Mar 2019 16:45:09 +0100 Subject: [PATCH 1/2] Add --allow-empty flag to merge command --- yq.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/yq.go b/yq.go index 700f15df66c..c05104764ba 100644 --- a/yq.go +++ b/yq.go @@ -22,6 +22,7 @@ var writeInplace = false var writeScript = "" var outputToJSON = false var overwriteFlag = false +var allowEmptyFlag = false var appendFlag = false var verbose = false var version = false @@ -229,6 +230,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(&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 } @@ -530,6 +532,9 @@ func mergeProperties(cmd *cobra.Command, args []string) error { for _, f := range filesToMerge { var fileToMerge interface{} if err := readData(f, 0, &fileToMerge); err != nil { + if allowEmptyFlag && err == io.EOF { + continue + } return nil, err } mapDataBucket["root"] = fileToMerge From 4b870ad656903f51ff4ff26339e55fa724189926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renzo=20Cris=C3=B3stomo?= Date: Thu, 21 Mar 2019 11:08:11 +0100 Subject: [PATCH 2/2] Add test for --allow-empty flag in merge command --- commands_test.go | 14 ++++++++++++++ examples/empty.yaml | 2 ++ 2 files changed, 16 insertions(+) create mode 100644 examples/empty.yaml diff --git a/commands_test.go b/commands_test.go index c228a146935..2e4924a422b 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1084,3 +1084,17 @@ c: assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n ")) assertResult(t, os.FileMode(int(0666)), info.Mode()) } + +func TestMergeAllowEmptyCmd(t *testing.T) { + cmd := getRootCommand() + result := runCmd(cmd, "merge --allow-empty examples/data1.yaml examples/empty.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `a: simple +b: +- 1 +- 2 +` + assertResult(t, expectedOutput, result.Output) +} diff --git a/examples/empty.yaml b/examples/empty.yaml new file mode 100644 index 00000000000..33fe031486d --- /dev/null +++ b/examples/empty.yaml @@ -0,0 +1,2 @@ +# a: apple +# b: cat