Skip to content

Commit

Permalink
ensure newline at end of formatted .env files
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 4, 2024
1 parent 8b71afa commit e62ba54
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
8 changes: 7 additions & 1 deletion pkg/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"fmt"
"io"
"os"

Expand All @@ -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
}
Expand Down
38 changes: 25 additions & 13 deletions pkg/render/line_buffer.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -25,7 +22,7 @@ func (lb *LineBuffer) AddPrinted(str string) bool {
return false
}

if str == "\n" {
if str == Newline {
str = ""
}

Expand All @@ -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
}
4 changes: 1 addition & 3 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type Renderer struct {
Output Outputter
Previous ast.Statement
Settings Settings

handlers []Handler
}

Expand All @@ -25,7 +24,6 @@ func NewRenderer(settings Settings, handlers ...Handler) *Renderer {
Output: output,
Previous: nil,
Settings: settings,

handlers: handlers,
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
KEY=VALUE

# My Second Key
FOO=BAZ
FOO=BAZ
1 change: 1 addition & 0 deletions pkg/render/test-fixtures/formatter/empty.golden.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Hello world
# Hello world
2 changes: 1 addition & 1 deletion pkg/render/test-fixtures/formatter/single-pair.golden.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
KEY=VALUE
KEY=VALUE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
KEY=VALUE
FOO=baz
FOO=baz
2 changes: 1 addition & 1 deletion pkg/render/test-fixtures/formatter/two-pairs.golden.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
KEY=VALUE
FOO=baz
FOO=baz

0 comments on commit e62ba54

Please sign in to comment.