Skip to content

Commit fc23871

Browse files
authored
Preventing comments from getting into entries (#74)
1 parent e29e649 commit fc23871

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pkg/parser/parser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
profileEnd = regexp.MustCompile(`(?i)# end\s*`)
1717
spaceRemover = regexp.MustCompile(`\s+`)
1818
tabReplacer = regexp.MustCompile(`\t+`)
19+
endingComment = regexp.MustCompile(`(.[^#]*).*`)
1920
)
2021

2122
// Parser is the interface for content parsers
@@ -142,8 +143,9 @@ func parseRouteLine(str string) (*types.Route, bool) {
142143
clean := spaceRemover.ReplaceAllString(str, " ")
143144
clean = tabReplacer.ReplaceAllString(clean, " ")
144145
clean = strings.TrimSpace(clean)
145-
146-
p := strings.Split(clean, " ")
146+
result := endingComment.FindStringSubmatch(clean)
147+
tResult := strings.TrimSpace(result[1])
148+
p := strings.Split(tResult, " ")
147149

148150
i := 0
149151
if p[0] == "#" && len(p) > 1 {

pkg/parser/parser_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func TestParser(t *testing.T) {
9393
}
9494
appendLine(p, "127.0.0.1 first.loc")
9595
appendLine(p, "127.0.0.1 second.loc")
96-
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 2)
96+
appendLine(p, "127.0.0.1 third.loc # test comment")
97+
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 3)
9798
})
9899

99100
t.Run("appendLine disabled", func(t *testing.T) {
@@ -102,7 +103,8 @@ func TestParser(t *testing.T) {
102103
Routes: map[string]*types.Route{},
103104
}
104105
appendLine(p, "# 127.0.0.1 first.loc")
105-
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 1)
106+
appendLine(p, "# 127.0.0.1 second.loc # test comment")
107+
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 2)
106108
})
107109

108110
t.Run("appendLine invalid lines", func(t *testing.T) {

0 commit comments

Comments
 (0)