From b41ceb87989874590580873d7e6b12991cab4e9e Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Sat, 21 Mar 2015 02:40:19 -0700 Subject: [PATCH] Removed even more unused code --- commands.go | 10 +++++----- hostfile.go | 53 ++++++++++++----------------------------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/commands.go b/commands.go index 1234330..9283a60 100644 --- a/commands.go +++ b/commands.go @@ -57,8 +57,8 @@ func Add(c *cli.Context) { hostname := NewHostname(c.Args()[0], c.Args()[1], true) var err error - if !hostsfile.Contains(hostname) { - err = hostsfile.Add(hostname) + if !hostsfile.Hosts.Contains(hostname) { + err = hostsfile.Hosts.Add(hostname) } if err == nil { @@ -81,9 +81,9 @@ func Del(c *cli.Context) { domain := c.Args()[0] hostsfile := MaybeLoadHostFile(c) - found := hostsfile.ContainsDomain(domain) + found := hostsfile.Hosts.ContainsDomain(domain) if found { - hostsfile.Delete(domain) + hostsfile.Hosts.RemoveDomain(domain) if c.Bool("n") { fmt.Println(hostsfile.Format()) } else { @@ -103,7 +103,7 @@ func Has(c *cli.Context) { domain := c.Args()[0] hostsfile := MaybeLoadHostFile(c) - found := hostsfile.ContainsDomain(domain) + found := hostsfile.Hosts.ContainsDomain(domain) if found { MaybePrintln(c, fmt.Sprintf("Found %s in %s", domain, GetHostsPath())) } else { diff --git a/hostfile.go b/hostfile.go index 2bb014b..cae38da 100644 --- a/hostfile.go +++ b/hostfile.go @@ -3,13 +3,11 @@ package hostess import ( "fmt" "io/ioutil" - "net" "os" - "sort" "strings" ) -const default_osx = ` +const defaultOSX = ` ## # Host Database # @@ -23,7 +21,7 @@ const default_osx = ` fe80::1%lo0 localhost ` -const default_linux = ` +const defaultLinux = ` 127.0.0.1 localhost 127.0.1.1 HOSTNAME @@ -41,29 +39,29 @@ ff02::3 ip6-allhosts type Hostfile struct { Path string Hosts Hostlist - data string + data []byte } -// NewHostFile creates a new Hostfile object from the specified file. +// NewHostfile creates a new Hostfile object from the specified file. func NewHostfile(path string) *Hostfile { - return &Hostfile{path, Hostlist{}, ""} + return &Hostfile{path, Hostlist{}, []byte{}} } -func (h *Hostfile) Load() string { +func (h *Hostfile) Load() []byte { data, err := ioutil.ReadFile(h.Path) if err != nil { - fmt.Println("Can't read ", h.Path) + fmt.Printf("Can't read %s: %s\n", h.Path, err) os.Exit(1) } - h.data = string(data) + h.data = data return h.data } func (h *Hostfile) Parse() []error { var errs []error - for _, v := range strings.Split(h.data, "\n") { + for _, v := range strings.Split(string(h.data), "\n") { for _, hostname := range ParseLine(v) { - err := h.Add(hostname) + err := h.Hosts.Add(hostname) if err != nil { errs = append(errs, err) } @@ -79,6 +77,8 @@ func LoadHostFile() (*Hostfile, []error) { return hostfile, errs } +// TrimWS (Trim Whitespace) removes space, newline, and tabs from a string +// using strings.Trim() func TrimWS(s string) string { return strings.Trim(s, " \n\t") } @@ -136,35 +136,6 @@ func MoveToFront(list []string, search string) []string { return append([]string{search}, list...) } -// ListDomainsByIP will look through Hostfile to find domains that match the -// specified IP and return them in a sorted slice. -func (h *Hostfile) ListDomainsByIP(ip net.IP) []string { - var names []string - for _, v := range h.Hosts { - if v.IP.Equal(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.Equal(net.ParseIP("127.0.0.1")) { - names = MoveToFront(names, "localhost") - } - - return names -} - -// ListDomains will return a list of domains in alphabetical order. -func (h *Hostfile) ListDomains() []string { - var names []string - for _, v := range h.Hosts { - names = append(names, v.Domain) - } - sort.Strings(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: