From e62ba54f85a85128c3689c4abf25ea7ec5fc8eb3 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sun, 4 Feb 2024 22:51:39 +0100 Subject: [PATCH] ensure newline at end of formatted .env files --- pkg/file.go | 8 +++- pkg/render/line_buffer.go | 38 ++++++++++++------- pkg/render/render.go | 4 +- .../formatter/comment-spacing.golden.env | 2 +- .../test-fixtures/formatter/empty.golden.env | 1 + .../formatter/just-a-comment.golden.env | 2 +- .../formatter/single-pair.golden.env | 2 +- .../formatter/two-pairs-newlines.golden.env | 2 +- .../formatter/two-pairs.golden.env | 2 +- 9 files changed, 39 insertions(+), 22 deletions(-) diff --git a/pkg/file.go b/pkg/file.go index 60ff374..3a10ea5 100644 --- a/pkg/file.go +++ b/pkg/file.go @@ -1,6 +1,7 @@ package pkg import ( + "fmt" "io" "os" @@ -27,7 +28,12 @@ func Save(filename string, doc *ast.Document) error { } defer f.Close() - _, err = f.WriteString(render.NewFormatter().Statement(doc)) + res := render.NewFormatter().Statement(doc) + if len(res) == 0 { + return fmt.Errorf("The rendered .env file is unexpectedly 0 bytes long - please report this as a bug (unless your file is empty)") + } + + _, err = f.WriteString(res) return err } diff --git a/pkg/render/line_buffer.go b/pkg/render/line_buffer.go index 4d28894..36ef4a8 100644 --- a/pkg/render/line_buffer.go +++ b/pkg/render/line_buffer.go @@ -1,21 +1,18 @@ package render -import "strings" +import ( + "strings" + "unicode" +) + +const Newline = "\n" type LineBuffer struct { lines []string } func (lb *LineBuffer) Add(str string) *LineBuffer { - if len(str) == 0 { - return lb - } - - if str == "\n" { - str = "" - } - - lb.lines = append(lb.lines, str) + lb.AddPrinted(str) return lb } @@ -25,7 +22,7 @@ func (lb *LineBuffer) AddPrinted(str string) bool { return false } - if str == "\n" { + if str == Newline { str = "" } @@ -35,9 +32,24 @@ func (lb *LineBuffer) AddPrinted(str string) bool { } func (lb *LineBuffer) Get() string { - return strings.Join(lb.lines, "\n") + return strings.Join(lb.lines, Newline) } -func (lb *LineBuffer) Newline() { +func (lb *LineBuffer) GetWithEOF() string { + return strings.TrimRightFunc(lb.Get(), unicode.IsSpace) + Newline +} + +func (lb *LineBuffer) Newline() *LineBuffer { lb.lines = append(lb.lines, "") + + return lb +} + +func (lb *LineBuffer) EnsureEOF() *LineBuffer { + idx := len(lb.lines) - 1 + if idx > 0 && lb.lines[idx] != "" { + return lb.Newline() + } + + return lb } diff --git a/pkg/render/render.go b/pkg/render/render.go index dc951b1..c67560a 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -10,7 +10,6 @@ type Renderer struct { Output Outputter Previous ast.Statement Settings Settings - handlers []Handler } @@ -25,7 +24,6 @@ func NewRenderer(settings Settings, handlers ...Handler) *Renderer { Output: output, Previous: nil, Settings: settings, - handlers: handlers, } } @@ -136,7 +134,7 @@ func (r *Renderer) Document(doc *ast.Document) string { return out. Add(r.Statement(doc.Statements)). Add(r.Statement(doc.Groups)). - Get() + GetWithEOF() } func (r *Renderer) Group(group *ast.Group) string { diff --git a/pkg/render/test-fixtures/formatter/comment-spacing.golden.env b/pkg/render/test-fixtures/formatter/comment-spacing.golden.env index d221241..e4a8bcd 100644 --- a/pkg/render/test-fixtures/formatter/comment-spacing.golden.env +++ b/pkg/render/test-fixtures/formatter/comment-spacing.golden.env @@ -2,4 +2,4 @@ KEY=VALUE # My Second Key -FOO=BAZ \ No newline at end of file +FOO=BAZ diff --git a/pkg/render/test-fixtures/formatter/empty.golden.env b/pkg/render/test-fixtures/formatter/empty.golden.env index e69de29..8b13789 100644 --- a/pkg/render/test-fixtures/formatter/empty.golden.env +++ b/pkg/render/test-fixtures/formatter/empty.golden.env @@ -0,0 +1 @@ + diff --git a/pkg/render/test-fixtures/formatter/just-a-comment.golden.env b/pkg/render/test-fixtures/formatter/just-a-comment.golden.env index c7c17d9..716ed14 100644 --- a/pkg/render/test-fixtures/formatter/just-a-comment.golden.env +++ b/pkg/render/test-fixtures/formatter/just-a-comment.golden.env @@ -1 +1 @@ -# Hello world \ No newline at end of file +# Hello world diff --git a/pkg/render/test-fixtures/formatter/single-pair.golden.env b/pkg/render/test-fixtures/formatter/single-pair.golden.env index 01da589..acd56aa 100644 --- a/pkg/render/test-fixtures/formatter/single-pair.golden.env +++ b/pkg/render/test-fixtures/formatter/single-pair.golden.env @@ -1 +1 @@ -KEY=VALUE \ No newline at end of file +KEY=VALUE diff --git a/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env b/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env index 928553b..0ee7e13 100644 --- a/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env +++ b/pkg/render/test-fixtures/formatter/two-pairs-newlines.golden.env @@ -1,2 +1,2 @@ KEY=VALUE -FOO=baz \ No newline at end of file +FOO=baz diff --git a/pkg/render/test-fixtures/formatter/two-pairs.golden.env b/pkg/render/test-fixtures/formatter/two-pairs.golden.env index 928553b..0ee7e13 100644 --- a/pkg/render/test-fixtures/formatter/two-pairs.golden.env +++ b/pkg/render/test-fixtures/formatter/two-pairs.golden.env @@ -1,2 +1,2 @@ KEY=VALUE -FOO=baz \ No newline at end of file +FOO=baz