diff --git a/hostfile.go b/hostfile.go index 6c8c83a..2dabd2b 100644 --- a/hostfile.go +++ b/hostfile.go @@ -166,80 +166,8 @@ func (h *Hostfile) GetData() []byte { // 2. Commented items are left in place // 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 -func (h *Hostfile) Format() string { - // localhost := "127.0.0.1 localhost" - - 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") +func (h *Hostfile) Format() []byte { + return h.Hosts.Format() } // Save writes the Hostfile to disk to /etc/hosts or to the location specified diff --git a/hostfile_test.go b/hostfile_test.go index 06e4127..f9d987f 100644 --- a/hostfile_test.go +++ b/hostfile_test.go @@ -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("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 { t.Errorf(asserts, expected, f) } diff --git a/hostlist.go b/hostlist.go index d055d45..6358508 100644 --- a/hostlist.go +++ b/hostlist.go @@ -296,11 +296,12 @@ func (h *Hostlist) FilterByDomainV(domain string, version int) (hostnames []*Hos // 2. Commented items are sorted displayed // 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 -func (h *Hostlist) Format() string { +func (h *Hostlist) Format() []byte { h.Sort() - out := "" + var out []byte for _, hostname := range *h { - out += hostname.Format() + "\n" + out = append(out, []byte(hostname.Format())...) + out = append(out, []byte("\n")...) } return out } diff --git a/hostlist_test.go b/hostlist_test.go index 98ff6b3..10f016d 100644 --- a/hostlist_test.go +++ b/hostlist_test.go @@ -50,7 +50,7 @@ func TestFormat(t *testing.T) { expected := `# 127.0.0.1 localhost 8.8.8.8 google.com ` - if hosts.Format() != expected { + if string(hosts.Format()) != expected { 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", "::1", true)) - fmt.Println(hosts.Format()) + fmt.Printf("%s\n", hosts.Format()) // Output: // # 127.0.0.1 google.com // ::1 google.com