diff --git a/Makefile b/Makefile index b9a6780..954d8e0 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ deps: go get build: deps - go build + go build cmd/hostess/hostess.go test: go test -coverprofile=coverage.out; go tool cover -html=coverage.out -o coverage.html diff --git a/cmd/hostess/hostess.go b/cmd/hostess/hostess.go index 778b58e..c24f49d 100644 --- a/cmd/hostess/hostess.go +++ b/cmd/hostess/hostess.go @@ -29,6 +29,7 @@ const help = `an idempotent tool for managing /etc/hosts func main() { app := cli.NewApp() app.Name = "hostess" + app.Authors = []cli.Author{{Name: "Chris Bednarski", Email: "banzaimonkey@gmail.com"}} app.Usage = help app.Version = "0.1.0" @@ -59,10 +60,11 @@ func main() { Flags: app.Flags, }, { - Name: "del", - Usage: "delete a hosts entry", - Action: hostess.Del, - Flags: app.Flags, + Name: "del", + Aliases: []string{"rm"}, + Usage: "delete a hosts entry", + Action: hostess.Del, + Flags: app.Flags, }, { Name: "has", @@ -83,10 +85,11 @@ func main() { Flags: app.Flags, }, { - Name: "ls, list", - Usage: "list entries in the hosts file", - Action: hostess.Ls, - Flags: app.Flags, + Name: "list", + Aliases: []string{"ls"}, + Usage: "list entries in the hosts file", + Action: hostess.Ls, + Flags: app.Flags, }, { Name: "fix", diff --git a/commands.go b/commands.go index e9d336f..68279ff 100644 --- a/commands.go +++ b/commands.go @@ -143,13 +143,12 @@ func Ls(c *cli.Context) { } } - // for _, domain := range hostsfile.ListDomains() { - // hostname := hostsfile.Hosts[domain] - // fmt.Printf("%s -> %s %s\n", - // StrPadRight(hostname.Domain, maxdomain), - // StrPadRight(hostname.Ip.String(), maxip), - // ShowEnabled(hostname.Enabled)) - // } + for _, hostname := range hostsfile.Hosts { + fmt.Printf("%s -> %s %s\n", + StrPadRight(hostname.Domain, maxdomain), + StrPadRight(hostname.IP.String(), maxip), + hostname.FormatEnabled()) + } } const fixHelp = `Programmatically rewrite your hostsfile. @@ -165,7 +164,7 @@ whitespace and comments will be removed. func Fix(c *cli.Context) { hostsfile := MaybeLoadHostFile(c) if c.Bool("n") { - fmt.Println(hostsfile.Format()) + fmt.Printf("%s", hostsfile.Format()) } else { hostsfile.Save() } diff --git a/hostfile.go b/hostfile.go index 2625eea..1f9cf93 100644 --- a/hostfile.go +++ b/hostfile.go @@ -139,6 +139,7 @@ func LoadHostfile() (hostfile *Hostfile, errs []error) { return } errs = hostfile.Parse() + hostfile.Hosts.Sort() return } diff --git a/hostlist.go b/hostlist.go index 40c4556..183aedb 100644 --- a/hostlist.go +++ b/hostlist.go @@ -49,14 +49,6 @@ func MakeSurrogateIP(IP net.IP) net.IP { // Less determines the sort order of two Hostnames, part of sort.Interface func (h Hostlist) Less(A, B int) bool { - // Sort "localhost" at the top - if h[A].Domain == "localhost" { - return true - } - if h[B].Domain == "localhost" { - return false - } - // Sort IPv4 before IPv6 // A is IPv4 and B is IPv6. A wins! if !h[A].IPv6 && h[B].IPv6 { @@ -67,6 +59,14 @@ func (h Hostlist) Less(A, B int) bool { return false } + // Sort "localhost" at the top + if h[A].Domain == "localhost" { + return true + } + if h[B].Domain == "localhost" { + return false + } + // Compare the the IP addresses (byte array) // We want to push 127. to the top so we're going to mark it zero. surrogateA := MakeSurrogateIP(h[A].IP)