Skip to content

Commit 480ed4c

Browse files
committed
keep-blank-lines: preserve different line endings
1 parent 49a84f8 commit 480ed4c

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

template.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"bytes"
65
"crypto/sha1"
76
"encoding/json"
@@ -383,7 +382,9 @@ func generateFile(config Config, containers Context) bool {
383382
contents := executeTemplate(config.Template, filteredContainers)
384383

385384
if !config.KeepBlankLines {
386-
contents = removeBlankLines(contents)
385+
buf := new(bytes.Buffer)
386+
removeBlankLines(bytes.NewReader(contents), buf)
387+
contents = buf.Bytes()
387388
}
388389

389390
if config.Dest != "" {
@@ -433,22 +434,10 @@ func executeTemplate(templatePath string, containers Context) []byte {
433434
log.Fatalf("unable to parse template: %s", err)
434435
}
435436

436-
var buf bytes.Buffer
437-
err = tmpl.ExecuteTemplate(&buf, filepath.Base(templatePath), &containers)
437+
buf := new(bytes.Buffer)
438+
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePath), &containers)
438439
if err != nil {
439440
log.Fatalf("template error: %s\n", err)
440441
}
441442
return buf.Bytes()
442443
}
443-
444-
func removeBlankLines(buf []byte) []byte {
445-
filtered := new(bytes.Buffer)
446-
scanner := bufio.NewScanner(bytes.NewReader(buf))
447-
for scanner.Scan() {
448-
line := scanner.Text()
449-
if !isBlank(line) {
450-
fmt.Fprintln(filtered, line)
451-
}
452-
}
453-
return filtered.Bytes()
454-
}

utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"bufio"
5+
"io"
46
"os"
57
"strings"
68
"unicode"
@@ -60,3 +62,22 @@ func isBlank(str string) bool {
6062
}
6163
return true
6264
}
65+
66+
func removeBlankLines(reader io.Reader, writer io.Writer) {
67+
breader := bufio.NewReader(reader)
68+
bwriter := bufio.NewWriter(writer)
69+
70+
for {
71+
line, err := breader.ReadString('\n')
72+
73+
if !isBlank(line) {
74+
bwriter.WriteString(line)
75+
}
76+
77+
if err != nil {
78+
break
79+
}
80+
}
81+
82+
bwriter.Flush()
83+
}

utils_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"bytes"
45
"flag"
56
"os"
7+
"strings"
68
"testing"
79
)
810

@@ -89,7 +91,6 @@ func TestSplitKeyValueSlice(t *testing.T) {
8991
}
9092

9193
func TestIsBlank(t *testing.T) {
92-
9394
tests := []struct {
9495
input string
9596
expected bool
@@ -113,3 +114,33 @@ func TestIsBlank(t *testing.T) {
113114
}
114115
}
115116
}
117+
118+
func TestRemoveBlankLines(t *testing.T) {
119+
tests := []struct {
120+
input string
121+
expected string
122+
}{
123+
{"", ""},
124+
{"\r\n\r\n", ""},
125+
{"line1\nline2", "line1\nline2"},
126+
{"line1\n\nline2", "line1\nline2"},
127+
{"\n\n\n\nline1\n\nline2", "line1\nline2"},
128+
{"\n\n\n\n\n \n \n \n", ""},
129+
130+
// windows line endings \r\n
131+
{"line1\r\nline2", "line1\r\nline2"},
132+
{"line1\r\n\r\nline2", "line1\r\nline2"},
133+
134+
// keep last new line
135+
{"line1\n", "line1\n"},
136+
{"line1\r\n", "line1\r\n"},
137+
}
138+
139+
for _, i := range tests {
140+
output := new(bytes.Buffer)
141+
removeBlankLines(strings.NewReader(i.input), output)
142+
if output.String() != i.expected {
143+
t.Fatalf("expected '%v'. got '%v'", i.expected, output)
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)