From 9423364cea2f240b18ac8f1d501eb3a81a16bf9c Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Wed, 4 Feb 2015 00:35:29 -0800 Subject: [PATCH] Added implementation for parseLine; missing LooksLikeIp --- hostess.go | 36 +++++++++++++++++++++++++++++++++--- hostess_test.go | 8 +++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/hostess.go b/hostess.go index 655f309..5500d92 100644 --- a/hostess.go +++ b/hostess.go @@ -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 } diff --git a/hostess_test.go b/hostess_test.go index e41cf8a..69a82cd 100644 --- a/hostess_test.go +++ b/hostess_test.go @@ -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)")