Added implementation for parseLine; missing LooksLikeIp

This commit is contained in:
Chris Bednarski 2015-02-04 00:35:29 -08:00
parent 06987f28c7
commit 9423364cea
2 changed files with 40 additions and 4 deletions

View File

@ -41,9 +41,36 @@ func TrimWS(s string) string {
return strings.Trim(s, " \n\t")
}
var line_parser = regexp.MustCompile(``)
func parseLine(line string) []Hostname {
hostnames := make([]Hostname, 0)
// Parse leading comment for disabled lines
enabled := true
if line[0:1] == "#" {
enabled = false
line = line[1:]
}
// Parse other comment for actual comments
line = strings.Split(line, "#")[0]
// Replace tabs and multispaces with single spaces throughout
line = strings.Replace(line, "\t", " ", -1)
line = strings.Replace(line, " ", " ", -1)
// Break line into words
words := strings.Split(line, " ")
// Separate the first bit (the ip) from the other bits (the domains)
ip := words[0]
domains := words[1:]
if LooksLikeIpv4(ip) || LooksLikeIpv6(ip) {
for _, v := range domains {
hostnames = append(hostnames, Hostname{v, ip, enabled})
}
}
// 1. Split on # to discard comments.
// - What if it's a legit line but commented out?
// - Disabled lines should have # at the beginning of the line
@ -54,15 +81,18 @@ func parseLine(line string) []Hostname {
// 3. Split remainder of line on whitespace to find
// domain names
// - Watch out for comments.
hostnames := make([]Hostname, 0)
return hostnames
}
var ipv4_pattern = regexp.MustCompile(`^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`)
func LooksLikeIpv4(ip string) bool {
return false
}
var ipv6_pattern = regexp.MustCompile(``)
func LooksLikeIpv6(ip string) bool {
return false
}

View File

@ -174,12 +174,18 @@ func TestListDomainsByIp(t *testing.T) {
func TestParseLine(t *testing.T) {
var hosts = []Hostname{}
// Commented stuff
// Comment
hosts = parseLine("# The following lines are desirable for IPv6 capable hosts")
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
// Single word comment
hosts = parseLine("#blah")
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
hosts = parseLine("#66.33.99.11 test.domain.com")
if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) {
t.Error("Expected to find test.domain.com (disabled)")