Moved hostess functionality into hostess package, added tests

pull/13/head
Chris Bednarski 9 years ago
parent ada8f87bd7
commit d809b75d3b

4
.gitignore vendored

@ -1 +1,3 @@
hostess
hostess
coverage.out
coverage.html

@ -2,7 +2,8 @@ build:
go build hostess.go
test: build
./hostess add domain ip
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
install: build
cp hostess /usr/sbin/hostess

@ -1,9 +1,11 @@
package main
package hostess
import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
)
const default_osx = `
@ -59,12 +61,22 @@ func (h *Hostfile) Read() string {
return h.data
}
func writeHosts(path string, contents string) {
func coalesce(hostnames []Hostname) string {
return ""
}
func writeHosts(path, contents string) {
}
func parseLine(line string) {
// return (Hostname, err)
var line_parser = regexp.MustCompile(``)
func parseLine(line string) (Hostname, error) {
hostname := Hostname{}
if false {
return hostname, errors.New("Can't parse hostname")
}
return hostname, nil
}
func parseHosts(hostfile string) []Hostname {
@ -94,7 +106,7 @@ func (h *Hostfile) Disable(domain string) {
}
}
func getHostsPath() string {
func GetHostsPath() string {
path := os.Getenv("HOSTESS_FILE")
if path == "" {
path = "/etc/hosts"
@ -110,8 +122,8 @@ func getArgs() []string {
return os.Args[2:]
}
func main() {
hostfile := NewHostfile(getHostsPath())
func Hostess() {
hostfile := NewHostfile(GetHostsPath())
hostfile.Read()
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
hostfile.Enable("localhost")

@ -0,0 +1,57 @@
package hostess
import (
"testing"
)
const ipv4_pass = `
127.0.0.1
127.0.1.1
10.200.30.50
99.99.99.99
999.999.999.999
0.1.1.0
`
const ipv4_fail = `
1234.1.1.1
123.5.6
12.12
76.76.67.67.45
`
const ipv6 = ``
func TestHostname(t *testing.T) {
const domain = "localhost"
const ip = "127.0.0.1"
const enabled = true
h := Hostname{}
h.Domain = domain
h.Ip = ip
h.Enabled = enabled
if h.Domain != domain {
t.Error("Domain should match " + domain)
}
if h.Ip != ip {
t.Error("Domain should match " + ip)
}
if h.Enabled != enabled {
t.Error("Enabled should be " + ip)
}
}
func TestGetHostsPath(t *testing.T) {
path := GetHostsPath()
const expected = "/etc/hosts"
if path != expected {
t.Error("Hosts path should be " + expected)
}
}
func TestHostfile(t *testing.T) {
hostfile := NewHostfile(GetHostsPath())
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
}

@ -0,0 +1,9 @@
package main
import (
"github.com/cbednarski/hostess"
)
func main() {
hostess.Hostess()
}
Loading…
Cancel
Save