Ignoring # comments in file discovery.

Signed-off-by: Yucong Sun <sunyucong@email.com>
This commit is contained in:
Yucong Sun 2015-04-04 22:28:58 +08:00 committed by test
parent 48fd99368f
commit b45a985b9b
2 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,17 @@ func (s *FileDiscoveryService) Initialize(path string, heartbeat int) error {
func parseFileContent(content []byte) []string {
var result []string
for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") {
line = strings.TrimSpace(line)
// Ignoring line starts with #
if strings.HasPrefix(line, "#") {
continue
}
// Inlined # comment also ignored.
if strings.Contains(line, "#") {
line = line[0:strings.Index(line, "#")]
// Trim additional spaces caused by above stripping.
line = strings.TrimSpace(line)
}
for _, ip := range discovery.Generate(line) {
result = append(result, ip)
}

View File

@ -29,3 +29,16 @@ func TestRegister(t *testing.T) {
discovery := &FileDiscoveryService{path: "/path/to/file"}
assert.Error(t, discovery.Register("0.0.0.0"))
}
func TestParsingContentsWithComments(t *testing.T) {
data := `
### test ###
1.1.1.1:1111 # inline comment
# 2.2.2.2:2222
### empty line with comment
### test ###
`
ips := parseFileContent([]byte(data))
assert.Equal(t, 1, len(ips))
assert.Equal(t, "1.1.1.1:1111", ips[0])
}