Clean up some golint errors

pull/13/head
Chris Bednarski 9 years ago
parent aa239820f5
commit ebc96283b0

@ -1,8 +1,6 @@
all: build test all: build test
deps: deps:
@go get golang.org/x/tools/cmd/cover
@go get golang.org/x/tools/cmd/vet
@go get github.com/golang/lint/golint @go get github.com/golang/lint/golint
@go get github.com/codegangsta/cli @go get github.com/codegangsta/cli

@ -44,9 +44,8 @@ func MaybeLoadHostFile(c *cli.Context) *Hostfile {
func ShowEnabled(on bool) string { func ShowEnabled(on bool) string {
if on { if on {
return "(On)" return "(On)"
} else {
return "(Off)"
} }
return "(Off)"
} }
// ShowHostname turns a Hostname into a string for display // ShowHostname turns a Hostname into a string for display
@ -60,6 +59,8 @@ func StrPadRight(s string, l int) string {
return s + strings.Repeat(" ", l-len(s)) return s + strings.Repeat(" ", l-len(s))
} }
// Add command parses <hostname> <ip> and adds or updates a hostname in the
// hosts file
func Add(c *cli.Context) { func Add(c *cli.Context) {
if len(c.Args()) != 2 { if len(c.Args()) != 2 {
MaybeError(c, "expected <hostname> <ip>") MaybeError(c, "expected <hostname> <ip>")
@ -85,6 +86,7 @@ func Add(c *cli.Context) {
} }
} }
// Del command removes any hostname(s) matching <domain> from the hosts file
func Del(c *cli.Context) { func Del(c *cli.Context) {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>") MaybeError(c, "expected <hostname>")
@ -106,6 +108,7 @@ func Del(c *cli.Context) {
} }
} }
// Has command indicates whether a hostname is present in the hosts file
func Has(c *cli.Context) { func Has(c *cli.Context) {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>") MaybeError(c, "expected <hostname>")
@ -122,6 +125,7 @@ func Has(c *cli.Context) {
} }
// Off command disables (comments) the specified hostname in the hosts file
func Off(c *cli.Context) { func Off(c *cli.Context) {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>") MaybeError(c, "expected <hostname>")
@ -129,12 +133,14 @@ func Off(c *cli.Context) {
} }
// On command enabled (uncomments) the specified hostname in the hosts file
func On(c *cli.Context) { func On(c *cli.Context) {
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
MaybeError(c, "expected <hostname>") MaybeError(c, "expected <hostname>")
} }
} }
// Ls command shows a list of hostnames in the hosts file
func Ls(c *cli.Context) { func Ls(c *cli.Context) {
hostsfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
maxdomain := 0 maxdomain := 0
@ -168,6 +174,7 @@ whitespace and comments will be removed.
hostess fix -n Show the new hostsfile. Don't write it hostess fix -n Show the new hostsfile. Don't write it
` `
// Fix command removes duplicates and conflicts from the hosts file
func Fix(c *cli.Context) { func Fix(c *cli.Context) {
hostsfile := MaybeLoadHostFile(c) hostsfile := MaybeLoadHostFile(c)
if c.Bool("n") { if c.Bool("n") {
@ -177,10 +184,12 @@ func Fix(c *cli.Context) {
} }
} }
// Dump command outputs hosts file contents as JSON
func Dump(c *cli.Context) { func Dump(c *cli.Context) {
} }
// Apply command adds hostnames to the hosts file from JSON
func Apply(c *cli.Context) { func Apply(c *cli.Context) {
} }

@ -7,11 +7,19 @@ import (
"sort" "sort"
) )
var InvalidVersionArgumentError = errors.New("Version argument must be 4 or 6") // ErrInvalidVersionArg is raised when a function expects IPv 4 or 6 but is
// passed a value not 4 or 6.
var ErrInvalidVersionArg = errors.New("Version argument must be 4 or 6")
// Hostlist is a collection of Hostnames. When in a Hostlist, hostnames must
// follow some rules:
// - Hostlist may contain IPv4 AND IPv6 (collectively, "IP version") hostnames.
// - Names are only allowed to overlap if IP version is different.
// - Adding a hostname for an existing name will replace the old one.
// See docs for the Sort and Add for more details.
type Hostlist []*Hostname type Hostlist []*Hostname
// NewHostList initializes a new Hostlist // NewHostlist initializes a new Hostlist
func NewHostlist() *Hostlist { func NewHostlist() *Hostlist {
return &Hostlist{} return &Hostlist{}
} }
@ -31,7 +39,7 @@ func (h Hostlist) Less(i, j int) bool {
return false return false
} }
// Sort ipv4 before ipv6 // Sort IPv4 before IPv6
if h[i].Ipv6 && !h[j].Ipv6 { if h[i].Ipv6 && !h[j].Ipv6 {
return false return false
} }
@ -39,8 +47,8 @@ func (h Hostlist) Less(i, j int) bool {
return true return true
} }
// Compare the the ip addresses (byte array) // Compare the the IP addresses (byte array)
for c, _ := range h[i].Ip { for c := range h[i].Ip {
if h[i].Ip[c] < h[j].Ip[c] { if h[i].Ip[c] < h[j].Ip[c] {
return true return true
} else if h[i].Ip[c] > h[j].Ip[c] { } else if h[i].Ip[c] > h[j].Ip[c] {
@ -87,8 +95,8 @@ func (h Hostlist) Swap(i, j int) {
// Sort this list of Hostnames, according to Hostlist sorting rules: // Sort this list of Hostnames, according to Hostlist sorting rules:
// 1. localhost comes first // 1. localhost comes first
// 2. ipv4 comes first // 2. IPv4 comes first
// 3. ips are sorted in numerical order // 3. IPs are sorted in numerical order
// 4. domains are sorted in alphabetical // 4. domains are sorted in alphabetical
func (h *Hostlist) Sort() { func (h *Hostlist) Sort() {
sort.Sort(*h) sort.Sort(*h)
@ -114,10 +122,10 @@ func (h *Hostlist) ContainsDomain(domain string) bool {
return false return false
} }
// ContainsDomain returns true if a Hostname in this Hostlist matches ip // ContainsIP returns true if a Hostname in this Hostlist matches IP
func (h *Hostlist) ContainsIp(ip net.IP) bool { func (h *Hostlist) ContainsIP(IP net.IP) bool {
for _, hostname := range *h { for _, hostname := range *h {
if hostname.EqualIp(ip) { if hostname.EqualIp(IP) {
return true return true
} }
} }
@ -125,7 +133,7 @@ func (h *Hostlist) ContainsIp(ip net.IP) bool {
} }
// Add a new Hostname to this hostlist. If a Hostname with the same domain name // Add a new Hostname to this hostlist. If a Hostname with the same domain name
// and Ip version is found, it will be replaced and an error will be returned. // and IP version is found, it will be replaced and an error will be returned.
// If you try to add an identical Hostname, an error will be returned. // If you try to add an identical Hostname, an error will be returned.
// Note that in normal operation, you will sometimes expect an error, and the // Note that in normal operation, you will sometimes expect an error, and the
// error data is mainly to alert you that you mis-entered information, not that // error data is mainly to alert you that you mis-entered information, not that
@ -133,11 +141,11 @@ func (h *Hostlist) ContainsIp(ip net.IP) bool {
func (h *Hostlist) Add(host *Hostname) error { func (h *Hostlist) Add(host *Hostname) error {
for _, found := range *h { for _, found := range *h {
if found.Equal(host) { if found.Equal(host) {
return errors.New(fmt.Sprintf("Duplicate hostname entry for %s -> %s", return fmt.Errorf("Duplicate hostname entry for %s -> %s",
host.Domain, host.Ip)) host.Domain, host.Ip)
} else if found.Domain == host.Domain && found.Ipv6 == host.Ipv6 { } else if found.Domain == host.Domain && found.Ipv6 == host.Ipv6 {
return errors.New(fmt.Sprintf("Conflicting hostname entries for %s -> %s and -> %s", return fmt.Errorf("Conflicting hostname entries for %s -> %s and -> %s",
host.Domain, host.Ip, found.Ip)) host.Domain, host.Ip, found.Ip)
} }
} }
*h = append(*h, host) *h = append(*h, host)
@ -160,7 +168,7 @@ func (h *Hostlist) IndexOf(host *Hostname) int {
// This function will panic if IP version is not 4 or 6. // This function will panic if IP version is not 4 or 6.
func (h *Hostlist) IndexOfDomainV(domain string, version int) int { func (h *Hostlist) IndexOfDomainV(domain string, version int) int {
if version != 4 && version != 6 { if version != 4 && version != 6 {
panic(InvalidVersionArgumentError) panic(ErrInvalidVersionArg)
} }
for index, hostname := range *h { for index, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) { if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
@ -188,7 +196,7 @@ func (h *Hostlist) RemoveDomain(domain string) {
// This function will panic if IP version is not 4 or 6. // This function will panic if IP version is not 4 or 6.
func (h *Hostlist) RemoveDomainV(domain string, version int) { func (h *Hostlist) RemoveDomainV(domain string, version int) {
if version != 4 && version != 6 { if version != 4 && version != 6 {
panic(InvalidVersionArgumentError) panic(ErrInvalidVersionArg)
} }
h.Remove(h.IndexOfDomainV(domain, version)) h.Remove(h.IndexOfDomainV(domain, version))
} }
@ -206,7 +214,7 @@ func (h *Hostlist) Enable(domain string) {
// This function will panic if IP version is not 4 or 6. // This function will panic if IP version is not 4 or 6.
func (h *Hostlist) EnableV(domain string, version int) { func (h *Hostlist) EnableV(domain string, version int) {
if version != 4 && version != 6 { if version != 4 && version != 6 {
panic(InvalidVersionArgumentError) panic(ErrInvalidVersionArg)
} }
for _, hostname := range *h { for _, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) { if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
@ -215,7 +223,7 @@ func (h *Hostlist) EnableV(domain string, version int) {
} }
} }
// Enable will change any Hostnames matching domain to be disabled. // Disable will change any Hostnames matching domain to be disabled.
func (h *Hostlist) Disable(domain string) { func (h *Hostlist) Disable(domain string) {
for _, hostname := range *h { for _, hostname := range *h {
if hostname.Domain == domain { if hostname.Domain == domain {
@ -224,11 +232,11 @@ func (h *Hostlist) Disable(domain string) {
} }
} }
// Enable will change any Hostnames matching domain and IP version to be disabled. // DisableV will change any Hostnames matching domain and IP version to be disabled.
// This function will panic if IP version is not 4 or 6. // This function will panic if IP version is not 4 or 6.
func (h *Hostlist) DisableV(domain string, version int) { func (h *Hostlist) DisableV(domain string, version int) {
if version != 4 && version != 6 { if version != 4 && version != 6 {
panic(InvalidVersionArgumentError) panic(ErrInvalidVersionArg)
} }
for _, hostname := range *h { for _, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) { if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {

@ -21,12 +21,12 @@ func TestContainsDomainIp(t *testing.T) {
} }
var first_ip = net.ParseIP(ip) var first_ip = net.ParseIP(ip)
if !hosts.ContainsIp(first_ip) { if !hosts.ContainsIP(first_ip) {
t.Errorf("Expected to find %s", ip) t.Errorf("Expected to find %s", ip)
} }
var extra_ip = net.ParseIP("1.2.3.4") var extra_ip = net.ParseIP("1.2.3.4")
if hosts.ContainsIp(extra_ip) { if hosts.ContainsIP(extra_ip) {
t.Errorf("Did not expect to find %s", extra_ip) t.Errorf("Did not expect to find %s", extra_ip)
} }

Loading…
Cancel
Save