1
0
mirror of https://github.com/docker/docs.git synced 2026-11-08 02:58:08 +03:00

Merge branch 'sunyc-ignore_comment'

This commit is contained in:
Victor Vieux
2015-04-07 10:41:25 -07:00
2 changed files with 26 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,18 @@ 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
3.3.3.3:3333
### test ###
`
ips := parseFileContent([]byte(data))
assert.Equal(t, 2, len(ips))
assert.Equal(t, "1.1.1.1:1111", ips[0])
assert.Equal(t, "3.3.3.3:3333", ips[1])
}