Fix implementation for ls

pull/13/head
Chris Bednarski 9 years ago
parent 93d4b5141b
commit d840489baa

@ -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

@ -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",

@ -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()
}

@ -139,6 +139,7 @@ func LoadHostfile() (hostfile *Hostfile, errs []error) {
return
}
errs = hostfile.Parse()
hostfile.Hosts.Sort()
return
}

@ -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)

Loading…
Cancel
Save