commit
85f33388b4
2 changed files with 59 additions and 19 deletions
|
@ -3,41 +3,55 @@ package etchosts
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultContent = map[string]string{
|
type Record struct {
|
||||||
"localhost": "127.0.0.1",
|
Hosts string
|
||||||
"localhost ip6-localhost ip6-loopback": "::1",
|
IP string
|
||||||
"ip6-localnet": "fe00::0",
|
|
||||||
"ip6-mcastprefix": "ff00::0",
|
|
||||||
"ip6-allnodes": "ff02::1",
|
|
||||||
"ip6-allrouters": "ff02::2",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Build(path, IP, hostname, domainname string, extraContent *map[string]string) error {
|
func (r Record) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
|
||||||
|
return int64(n), err
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultContent = []Record{
|
||||||
|
{Hosts: "localhost", IP: "127.0.0.1"},
|
||||||
|
{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
|
||||||
|
{Hosts: "ip6-localnet", IP: "fe00::0"},
|
||||||
|
{Hosts: "ip6-mcastprefix", IP: "ff00::0"},
|
||||||
|
{Hosts: "ip6-allnodes", IP: "ff02::1"},
|
||||||
|
{Hosts: "ip6-allrouters", IP: "ff02::2"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func Build(path, IP, hostname, domainname string, extraContent []Record) error {
|
||||||
content := bytes.NewBuffer(nil)
|
content := bytes.NewBuffer(nil)
|
||||||
if IP != "" {
|
if IP != "" {
|
||||||
|
var mainRec Record
|
||||||
|
mainRec.IP = IP
|
||||||
if domainname != "" {
|
if domainname != "" {
|
||||||
content.WriteString(fmt.Sprintf("%s\t%s.%s %s\n", IP, hostname, domainname, hostname))
|
mainRec.Hosts = fmt.Sprintf("%s.%s %s", hostname, domainname, hostname)
|
||||||
} else {
|
} else {
|
||||||
content.WriteString(fmt.Sprintf("%s\t%s\n", IP, hostname))
|
mainRec.Hosts = hostname
|
||||||
}
|
}
|
||||||
}
|
if _, err := mainRec.WriteTo(content); err != nil {
|
||||||
|
|
||||||
for hosts, ip := range defaultContent {
|
|
||||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if extraContent != nil {
|
for _, r := range defaultContent {
|
||||||
for hosts, ip := range *extraContent {
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, r := range extraContent {
|
||||||
|
if _, err := r.WriteTo(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
||||||
|
|
|
@ -7,6 +7,32 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestBuildDefault(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
// check that /etc/hosts has consistent ordering
|
||||||
|
for i := 0; i <= 5; i++ {
|
||||||
|
err = Build(file.Name(), "", "", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
expected := "127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\nfe00::0\tip6-localnet\nff00::0\tip6-mcastprefix\nff02::1\tip6-allnodes\nff02::2\tip6-allrouters\n"
|
||||||
|
|
||||||
|
if expected != string(content) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuildHostnameDomainname(t *testing.T) {
|
func TestBuildHostnameDomainname(t *testing.T) {
|
||||||
file, err := ioutil.TempFile("", "")
|
file, err := ioutil.TempFile("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue