Added TrimWS (whitespace) and ListDomainsByIp

pull/13/head
Chris Bednarski 9 years ago
parent 0c04cfa0dd
commit 3d407ae410

@ -60,6 +60,10 @@ type Hostfile struct {
data string
}
func TrimWS(s string) string {
return strings.Trim(s, " \n\t")
}
// NewHostFile creates a new Hostfile object from the specified file.
func NewHostfile(path string) *Hostfile {
return &Hostfile{path, make(map[string]*Hostname), ""}
@ -102,12 +106,34 @@ func getSortedMapKeys(m map[string][]string) []string {
keys[i] = k
i += 1
}
keys = sort.StringSlice(keys)
sort.Strings(keys)
return keys
}
// Dump takes the current list of Hostnames in this Hostfile and turns it into
// a string suitable for use as an /etc/hosts file.
func (h *Hostfile) ListDomainsByIp(ip string) []string {
names := make([]string, 0)
for _, v := range h.Hosts {
if v.Ip == ip {
names = append(names, v.Domain)
}
}
sort.Strings(names)
// Magic for localhost only, to make sure it's the first domain on its line
if ip == "127.0.0.1" {
for k, v := range names {
if v == "localhost" {
names = append(names[:k], names[k+1:]...)
}
}
names = append([]string{"localhost"}, names...)
}
return names
}
// Format takes the current list of Hostnames in this Hostfile and turns it
// into a string suitable for use as an /etc/hosts file.
// Sorting uses the following logic:
// 1. List is sorted by IP address
// 2. Commented items are left in place

@ -115,11 +115,14 @@ func TestFormatHostname(t *testing.T) {
}
func TestFormatHostfile(t *testing.T) {
// t.Skip("Not implemented")
// The sort order here is a bit weird.
// 1. We want localhost entries at the top
// 2. The rest are sorted by IP as STRINGS, not numeric values, so 10
// precedes 8
const expected = `127.0.0.1 localhost devsite
127.0.1.1 ip-10-37-12-18
# 8.8.8.8 google.com
10.37.12.18 devsite.com m.devsite.com`
10.37.12.18 devsite.com m.devsite.com
# 8.8.8.8 google.com`
hostfile := NewHostfile("./hosts")
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
@ -128,7 +131,40 @@ func TestFormatHostfile(t *testing.T) {
hostfile.Add(Hostname{"google.com", "8.8.8.8", false})
hostfile.Add(Hostname{"devsite.com", "10.37.12.18", true})
hostfile.Add(Hostname{"m.devsite.com", "10.37.12.18", true})
if hostfile.Format() != expected {
t.Errorf(asserts, expected, hostfile.Format())
f := hostfile.Format()
if f != expected {
t.Errorf(asserts, expected, f)
}
}
func TestTrimWS(t *testing.T) {
const expected = ` candy
`
got := TrimWS(expected)
if got != "candy" {
t.Errorf(asserts, expected, got)
}
}
func TestListDomainsByIp(t *testing.T) {
hostfile := NewHostfile("./hosts")
hostfile.Add(Hostname{"devsite.com", "10.37.12.18", true})
hostfile.Add(Hostname{"m.devsite.com", "10.37.12.18", true})
hostfile.Add(Hostname{"google.com", "8.8.8.8", false})
names := hostfile.ListDomainsByIp("10.37.12.18")
if !(names[0] == "devsite.com" && names[1] == "m.devsite.com") {
t.Errorf("Expected devsite.com and m.devsite.com. Got %s", names)
}
hostfile2 := NewHostfile("./hosts")
hostfile2.Add(Hostname{"localhost", "127.0.0.1", true})
hostfile2.Add(Hostname{"ip-10-37-12-18", "127.0.1.1", true})
hostfile2.Add(Hostname{"devsite", "127.0.0.1", true})
names2 := hostfile2.ListDomainsByIp("127.0.0.1")
if !(names2[0] == "localhost" && names2[1] == "devsite") {
t.Errorf("Expected localhost and devsite. Got %s", names2)
}
}

Loading…
Cancel
Save