From f1dbe13f212099b997d77672e64593c078f5c0bb Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 4 Feb 2020 14:21:54 +1100 Subject: [PATCH] Can write and merge into empty files :) --- cmd/commands_test.go | 16 ++++++++++++++++ cmd/utils.go | 7 ++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index a9d70fdc..62231e44 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -1101,6 +1101,22 @@ func TestWriteCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestWriteEmptyCmd(t *testing.T) { + content := `` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c 7", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: 7 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestWriteCmdScript(t *testing.T) { content := `b: c: 3 diff --git a/cmd/utils.go b/cmd/utils.go index d4e473c4..14e9a9f3 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -188,7 +188,12 @@ func mapYamlDecoder(updateData updateDataFn, encoder yqlib.Encoder) yamlDecoderF log.Debugf("Read doc %v", currentIndex) errorReading = decoder.Decode(&dataBucket) - if errorReading == io.EOF { + if errorReading == io.EOF && docIndexInt == 0 && currentIndex == 0 { + //empty document, lets just make one + child := yaml.Node{Kind: yaml.MappingNode} + dataBucket = yaml.Node{Kind: yaml.DocumentNode, Content: make([]*yaml.Node, 1)} + dataBucket.Content[0] = &child + } else if errorReading == io.EOF { if !updateAll && currentIndex <= docIndexInt { return fmt.Errorf("asked to process document index %v but there are only %v document(s)", docIndex, currentIndex) }