Changed to *V commands to switch between ipv4 and ipv6 functionality

pull/13/head
Chris Bednarski 9 years ago
parent 293dcf6bad
commit 7f4f5d2b2e

@ -3,15 +3,17 @@ all: build test
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/codegangsta/cli
build: deps
go build
test:
go test -coverprofile=../coverage.out
go tool cover -html=../coverage.out -o ../coverage.html
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
go vet
golint
gox:
@go get github.com/mitchellh/gox
@ -25,5 +27,6 @@ install: build test
cp hostess /usr/sbin/hostess
clean:
rm ./hostess
rm ./hostess_*
rm -f ./hostess
rm -f ./hostess_*
rm -f ./coverage.*

@ -12,6 +12,8 @@ import (
// Sort
// Other things that maybe implemented in hostfile
var InvalidVersionArgumentError = errors.New("Version argument must be 4 or 6")
type Hostlist []*Hostname
func NewHostlist() *Hostlist {
@ -77,18 +79,9 @@ func (h *Hostlist) IndexOf(host *Hostname) int {
return -1
}
func (h *Hostlist) IndexOfDomainIpv4(domain string) int {
for index, found := range *h {
if found.Domain == domain && found.Ipv6 == false {
return index
}
}
return -1
}
func (h *Hostlist) IndexOfDomainIpv6(domain string) int {
for index, found := range *h {
if found.Domain == domain && found.Ipv6 == true {
func (h *Hostlist) IndexOfDomainV(domain string, version int) int {
for index, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
return index
}
}
@ -96,15 +89,21 @@ func (h *Hostlist) IndexOfDomainIpv6(domain string) int {
}
func (h *Hostlist) Remove(index int) {
*h = append((*h)[:index], (*h)[index+1:]...)
if index > -1 && index < len(*h) {
*h = append((*h)[:index], (*h)[index+1:]...)
}
}
func (h *Hostlist) RemoveIpv4Domain(domain string) {
h.Remove(h.IndexOfDomainIpv4(domain))
func (h *Hostlist) RemoveDomain(domain string) {
h.Remove(h.IndexOfDomainV(domain, 4))
h.Remove(h.IndexOfDomainV(domain, 6))
}
func (h *Hostlist) RemoveIpv6Domain(domain string) {
h.Remove(h.IndexOfDomainIpv6(domain))
func (h *Hostlist) RemoveDomainV(domain string, version int) {
if version != 4 && version != 6 {
panic(InvalidVersionArgumentError)
}
h.Remove(h.IndexOfDomainV(domain, version))
}
func (h *Hostlist) Enable(domain string) {
@ -115,6 +114,14 @@ func (h *Hostlist) Enable(domain string) {
}
}
func (h *Hostlist) EnableV(domain string, version int) {
for _, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
hostname.Enabled = true
}
}
}
func (h *Hostlist) Disable(domain string) {
for _, hostname := range *h {
if hostname.Domain == domain {
@ -123,6 +130,14 @@ func (h *Hostlist) Disable(domain string) {
}
}
func (h *Hostlist) DisableV(domain string, version int) {
for _, hostname := range *h {
if hostname.Domain == domain && hostname.Ipv6 == (version == 6) {
hostname.Enabled = false
}
}
}
func (h *Hostlist) Copy() *Hostlist {
var n *Hostlist
copy(*h, *n)

@ -69,14 +69,14 @@ func TestRemove(t *testing.T) {
}
}
func RemoveIpvXDomain(t *testing.T) {
func TestRemoveDomain(t *testing.T) {
hosts := hostess.NewHostlist()
h1 := hostess.NewHostname("google.com", "127.0.0.1", false)
h2 := hostess.NewHostname("google.com", "::1", true)
hosts.Add(h1)
hosts.Add(h2)
hosts.RemoveIpv4Domain("google.com")
hosts.RemoveDomainV("google.com", 4)
if hosts.ContainsHostname(h1) {
t.Error("Should not contain ipv4 hostname")
}
@ -84,7 +84,7 @@ func RemoveIpvXDomain(t *testing.T) {
t.Error("Should still contain ipv6 hostname")
}
hosts.RemoveIpv6Domain("google.com")
hosts.RemoveDomainV("google.com", 6)
if len(*hosts) != 0 {
t.Error("Should no longer contain any hostnames")
}

Loading…
Cancel
Save