Skip to content

Commit fd1228d

Browse files
authored
Merge pull request #3 from adityadani/master
Handle trailing comments on a hosts file line.
2 parents 01dac41 + 2430ff0 commit fd1228d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

txeh.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type HostFileLine struct {
4545
Hostnames []string
4646
Raw string
4747
Trimed string
48+
Comment string
4849
}
4950

5051
// NewHostsDefault returns a hosts object with
@@ -322,6 +323,12 @@ func ParseHosts(path string) ([]HostFileLine, error) {
322323
continue
323324
}
324325

326+
curLineSplit := strings.SplitN(curLine.Trimed, "#", 2)
327+
if len(curLineSplit) > 1 {
328+
curLine.Comment = curLineSplit[1]
329+
}
330+
curLine.Trimed = curLineSplit[0]
331+
325332
curLine.Parts = strings.Fields(curLine.Trimed)
326333

327334
if len(curLine.Parts) > 1 {
@@ -361,5 +368,8 @@ func lineFormatter(hfl HostFileLine) string {
361368
return hfl.Raw
362369
}
363370

371+
if len(hfl.Comment) > 0 {
372+
return fmt.Sprintf("%-16s %s #%s", hfl.Address, strings.Join(hfl.Hostnames, " "), hfl.Comment)
373+
}
364374
return fmt.Sprintf("%-16s %s", hfl.Address, strings.Join(hfl.Hostnames, " "))
365375
}

0 commit comments

Comments
 (0)