mirror of
https://github.com/cbednarski/hostess
synced 2024-11-16 00:13:01 +00:00
Changed format to use byte arrays
This commit is contained in:
parent
60c5e85bc5
commit
c2dc0f5272
76
hostfile.go
76
hostfile.go
@ -166,80 +166,8 @@ func (h *Hostfile) GetData() []byte {
|
|||||||
// 2. Commented items are left in place
|
// 2. Commented items are left in place
|
||||||
// 3. 127.* appears at the top of the list (so boot resolvers don't break)
|
// 3. 127.* appears at the top of the list (so boot resolvers don't break)
|
||||||
// 4. When present, localhost will always appear first in the domain list
|
// 4. When present, localhost will always appear first in the domain list
|
||||||
func (h *Hostfile) Format() string {
|
func (h *Hostfile) Format() []byte {
|
||||||
// localhost := "127.0.0.1 localhost"
|
return h.Hosts.Format()
|
||||||
|
|
||||||
localhosts := make(map[string][]string)
|
|
||||||
ips := make(map[string][]string)
|
|
||||||
|
|
||||||
// Map domains and IPs into slices of domains keyd by IP
|
|
||||||
// 127.0.0.1 = [localhost, blah, blah2]
|
|
||||||
// 2.2.2.3 = [domain1, domain2]
|
|
||||||
for _, hostname := range h.Hosts {
|
|
||||||
if hostname.IP.String()[0:4] == "127." {
|
|
||||||
localhosts[hostname.IP.String()] = append(localhosts[hostname.IP.String()], hostname.Domain)
|
|
||||||
} else {
|
|
||||||
ips[hostname.IP.String()] = append(ips[hostname.IP.String()], hostname.Domain)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// localhosts_keys := getSortedMapKeys(localhosts)
|
|
||||||
// ips_keys := getSortedMapKeys(ips)
|
|
||||||
var out []string
|
|
||||||
|
|
||||||
// for _, ip := range localhosts_keys {
|
|
||||||
// enabled := ip
|
|
||||||
// enabled_b := false
|
|
||||||
// disabled := "# " + ip
|
|
||||||
// disabled_b := false
|
|
||||||
// IP := net.ParseIP(ip)
|
|
||||||
// for _, domain := range h.ListDomainsByIP(IP) {
|
|
||||||
// hostname := *h.Hosts[domain]
|
|
||||||
// if hostname.IP.Equal(IP) {
|
|
||||||
// if hostname.Enabled {
|
|
||||||
// enabled += " " + hostname.Domain
|
|
||||||
// enabled_b = true
|
|
||||||
// } else {
|
|
||||||
// disabled += " " + hostname.Domain
|
|
||||||
// disabled_b = true
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if enabled_b {
|
|
||||||
// out = append(out, enabled)
|
|
||||||
// }
|
|
||||||
// if disabled_b {
|
|
||||||
// out = append(out, disabled)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for _, ip := range ips_keys {
|
|
||||||
// enabled := ip
|
|
||||||
// enabled_b := false
|
|
||||||
// disabled := "# " + ip
|
|
||||||
// disabled_b := false
|
|
||||||
// IP := net.ParseIP(ip)
|
|
||||||
// for _, domain := range h.ListDomainsByIP(IP) {
|
|
||||||
// hostname := *h.Hosts[domain]
|
|
||||||
// if hostname.IP.Equal(IP) {
|
|
||||||
// if hostname.Enabled {
|
|
||||||
// enabled += " " + hostname.Domain
|
|
||||||
// enabled_b = true
|
|
||||||
// } else {
|
|
||||||
// disabled += " " + hostname.Domain
|
|
||||||
// disabled_b = true
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if enabled_b {
|
|
||||||
// out = append(out, enabled)
|
|
||||||
// }
|
|
||||||
// if disabled_b {
|
|
||||||
// out = append(out, disabled)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
return strings.Join(out, "\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save writes the Hostfile to disk to /etc/hosts or to the location specified
|
// Save writes the Hostfile to disk to /etc/hosts or to the location specified
|
||||||
|
@ -59,7 +59,7 @@ func TestFormatHostfile(t *testing.T) {
|
|||||||
hostfile.Hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
|
hostfile.Hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
|
||||||
hostfile.Hosts.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
|
hostfile.Hosts.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
|
||||||
hostfile.Hosts.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
|
hostfile.Hosts.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
|
||||||
f := hostfile.Format()
|
f := string(hostfile.Format())
|
||||||
if f != expected {
|
if f != expected {
|
||||||
t.Errorf(asserts, expected, f)
|
t.Errorf(asserts, expected, f)
|
||||||
}
|
}
|
||||||
|
@ -296,11 +296,12 @@ func (h *Hostlist) FilterByDomainV(domain string, version int) (hostnames []*Hos
|
|||||||
// 2. Commented items are sorted displayed
|
// 2. Commented items are sorted displayed
|
||||||
// 3. 127.* appears at the top of the list (so boot resolvers don't break)
|
// 3. 127.* appears at the top of the list (so boot resolvers don't break)
|
||||||
// 4. When present, "localhost" will always appear first in the domain list
|
// 4. When present, "localhost" will always appear first in the domain list
|
||||||
func (h *Hostlist) Format() string {
|
func (h *Hostlist) Format() []byte {
|
||||||
h.Sort()
|
h.Sort()
|
||||||
out := ""
|
var out []byte
|
||||||
for _, hostname := range *h {
|
for _, hostname := range *h {
|
||||||
out += hostname.Format() + "\n"
|
out = append(out, []byte(hostname.Format())...)
|
||||||
|
out = append(out, []byte("\n")...)
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func TestFormat(t *testing.T) {
|
|||||||
expected := `# 127.0.0.1 localhost
|
expected := `# 127.0.0.1 localhost
|
||||||
8.8.8.8 google.com
|
8.8.8.8 google.com
|
||||||
`
|
`
|
||||||
if hosts.Format() != expected {
|
if string(hosts.Format()) != expected {
|
||||||
t.Error("Formatted hosts list is not formatted correctly")
|
t.Error("Formatted hosts list is not formatted correctly")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,7 +146,7 @@ func ExampleHostlist_1() {
|
|||||||
hosts.Add(hostess.NewHostname("google.com", "127.0.0.1", false))
|
hosts.Add(hostess.NewHostname("google.com", "127.0.0.1", false))
|
||||||
hosts.Add(hostess.NewHostname("google.com", "::1", true))
|
hosts.Add(hostess.NewHostname("google.com", "::1", true))
|
||||||
|
|
||||||
fmt.Println(hosts.Format())
|
fmt.Printf("%s\n", hosts.Format())
|
||||||
// Output:
|
// Output:
|
||||||
// # 127.0.0.1 google.com
|
// # 127.0.0.1 google.com
|
||||||
// ::1 google.com
|
// ::1 google.com
|
||||||
|
Loading…
Reference in New Issue
Block a user