Removed even more unused code

pull/13/head
Chris Bednarski 9 years ago
parent 17a11828bb
commit b41ceb8798

@ -57,8 +57,8 @@ func Add(c *cli.Context) {
hostname := NewHostname(c.Args()[0], c.Args()[1], true) hostname := NewHostname(c.Args()[0], c.Args()[1], true)
var err error var err error
if !hostsfile.Contains(hostname) { if !hostsfile.Hosts.Contains(hostname) {
err = hostsfile.Add(hostname) err = hostsfile.Hosts.Add(hostname)
} }
if err == nil { if err == nil {
@ -81,9 +81,9 @@ func Del(c *cli.Context) {
domain := c.Args()[0] domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
found := hostsfile.ContainsDomain(domain) found := hostsfile.Hosts.ContainsDomain(domain)
if found { if found {
hostsfile.Delete(domain) hostsfile.Hosts.RemoveDomain(domain)
if c.Bool("n") { if c.Bool("n") {
fmt.Println(hostsfile.Format()) fmt.Println(hostsfile.Format())
} else { } else {
@ -103,7 +103,7 @@ func Has(c *cli.Context) {
domain := c.Args()[0] domain := c.Args()[0]
hostsfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
found := hostsfile.ContainsDomain(domain) found := hostsfile.Hosts.ContainsDomain(domain)
if found { if found {
MaybePrintln(c, fmt.Sprintf("Found %s in %s", domain, GetHostsPath())) MaybePrintln(c, fmt.Sprintf("Found %s in %s", domain, GetHostsPath()))
} else { } else {

@ -3,13 +3,11 @@ package hostess
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"os" "os"
"sort"
"strings" "strings"
) )
const default_osx = ` const defaultOSX = `
## ##
# Host Database # Host Database
# #
@ -23,7 +21,7 @@ const default_osx = `
fe80::1%lo0 localhost fe80::1%lo0 localhost
` `
const default_linux = ` const defaultLinux = `
127.0.0.1 localhost 127.0.0.1 localhost
127.0.1.1 HOSTNAME 127.0.1.1 HOSTNAME
@ -41,29 +39,29 @@ ff02::3 ip6-allhosts
type Hostfile struct { type Hostfile struct {
Path string Path string
Hosts Hostlist 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 { 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) data, err := ioutil.ReadFile(h.Path)
if err != nil { if err != nil {
fmt.Println("Can't read ", h.Path) fmt.Printf("Can't read %s: %s\n", h.Path, err)
os.Exit(1) os.Exit(1)
} }
h.data = string(data) h.data = data
return h.data return h.data
} }
func (h *Hostfile) Parse() []error { func (h *Hostfile) Parse() []error {
var errs []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) { for _, hostname := range ParseLine(v) {
err := h.Add(hostname) err := h.Hosts.Add(hostname)
if err != nil { if err != nil {
errs = append(errs, err) errs = append(errs, err)
} }
@ -79,6 +77,8 @@ func LoadHostFile() (*Hostfile, []error) {
return hostfile, errs return hostfile, errs
} }
// TrimWS (Trim Whitespace) removes space, newline, and tabs from a string
// using strings.Trim()
func TrimWS(s string) string { func TrimWS(s string) string {
return strings.Trim(s, " \n\t") return strings.Trim(s, " \n\t")
} }
@ -136,35 +136,6 @@ func MoveToFront(list []string, search string) []string {
return append([]string{search}, list...) 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 // Format takes the current list of Hostnames in this Hostfile and turns it
// into a string suitable for use as an /etc/hosts file. // into a string suitable for use as an /etc/hosts file.
// Sorting uses the following logic: // Sorting uses the following logic:

Loading…
Cancel
Save