From b45a985b9b492cdb91d384e1d3ea05c273b414c2 Mon Sep 17 00:00:00 2001 From: Yucong Sun Date: Sat, 4 Apr 2015 22:28:58 +0800 Subject: [PATCH] Ignoring # comments in file discovery. Signed-off-by: Yucong Sun --- discovery/file/file.go | 11 +++++++++++ discovery/file/file_test.go | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/discovery/file/file.go b/discovery/file/file.go index 852bbb0bd6..bda7465ed0 100644 --- a/discovery/file/file.go +++ b/discovery/file/file.go @@ -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) } diff --git a/discovery/file/file_test.go b/discovery/file/file_test.go index 961731c7a8..6deede99fc 100644 --- a/discovery/file/file_test.go +++ b/discovery/file/file_test.go @@ -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]) +}