Browse Source

Add field to separate the section from the rest

tags/v3.1.0
Chris Wiegman 5 months ago
parent
commit
1508dfc093
Signed by: chriswiegman <chris@chriswiegman.com> GPG Key ID: 2B23D7A74FDE2DF4
4 changed files with 37 additions and 32 deletions
  1. +2
    -2
      cmd/main.go
  2. +5
    -2
      goodhosts.go
  3. +13
    -11
      hostsFile.go
  4. +17
    -17
      hostsFile_test.go

+ 2
- 2
cmd/main.go View File

@@ -32,12 +32,12 @@ Options:

args, _ := docopt.Parse(usage, nil, true, "Goodhosts 3.0.1", false)

hosts, err := goodhosts.NewHosts()
hosts, err := goodhosts.NewHosts("")
check(err)

if args["list"].(bool) {
total := 0
for _, line := range hosts.Lines {
for _, line := range hosts.FileLines {
var lineOutput string

if goodhosts.IsComment(line.Raw) && !args["--all"].(bool) {


+ 5
- 2
goodhosts.go View File

@@ -6,7 +6,7 @@ import (
)

// NewHosts Return a new instance of ``Hosts``.
func NewHosts() (Hosts, error) {
func NewHosts(sectionName string) (Hosts, error) {
osHostsFilePath := ""

if os.Getenv("HOSTS_PATH") == "" {
@@ -15,7 +15,10 @@ func NewHosts() (Hosts, error) {
osHostsFilePath = os.Getenv("HOSTS_PATH")
}

hosts := Hosts{Path: osHostsFilePath}
hosts := Hosts{
Path: osHostsFilePath,
Section: sectionName,
}

err := hosts.Load()
if err != nil {


+ 13
- 11
hostsFile.go View File

@@ -9,8 +9,10 @@ import (

// Hosts Represents a hosts file.
type Hosts struct {
Path string
Lines []HostsLine
Path string
Section string
FileLines []HostsLine
SectionLines []HostsLine
}

// IsWritable Return ```true``` if hosts file is writable.
@@ -49,7 +51,7 @@ func (h *Hosts) Load() error {
return err
}

h.Lines = lines
h.FileLines = lines

return nil
}
@@ -64,7 +66,7 @@ func (h Hosts) Flush() error {

w := bufio.NewWriter(file)

for _, line := range h.Lines {
for _, line := range h.FileLines {
fmt.Fprintf(w, "%s%s", line.Raw, eol)
}

@@ -88,7 +90,7 @@ func (h *Hosts) Add(ip, comment string, hosts ...string) error {
if !h.Has(ip, host) {
endLine := NewHostsLine(buildRawLine(ip, host, comment))
endLine.Comment = comment
h.Lines = append(h.Lines, endLine)
h.FileLines = append(h.FileLines, endLine)
}
}

@@ -110,7 +112,7 @@ func (h *Hosts) Remove(ip string, hosts ...string) error {
return fmt.Errorf("%q is an invalid IP address", ip)
}

for _, line := range h.Lines {
for _, line := range h.FileLines {

// Bad lines or comments just get readded.
if line.Err != nil || IsComment(line.Raw) || line.IP != ip {
@@ -142,14 +144,14 @@ func (h *Hosts) Remove(ip string, hosts ...string) error {
}
}

h.Lines = outputLines
h.FileLines = outputLines
return nil
}

func (h Hosts) getHostPosition(ip string, host string) int {

for i := range h.Lines {
line := h.Lines[i]
for i := range h.FileLines {
line := h.FileLines[i]
if !IsComment(line.Raw) && line.Raw != "" {
if ip == line.IP && itemInSlice(host, line.Hosts) {
return i
@@ -161,8 +163,8 @@ func (h Hosts) getHostPosition(ip string, host string) int {
}

func (h Hosts) getIPPosition(ip string) int {
for i := range h.Lines {
line := h.Lines[i]
for i := range h.FileLines {
line := h.FileLines[i]
if !IsComment(line.Raw) && line.Raw != "" {
if line.IP == ip {
return i


+ 17
- 17
hostsFile_test.go View File

@@ -7,7 +7,7 @@ import (

func TestHostsHas(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 nada"),
}
@@ -25,7 +25,7 @@ func TestHostsHas(t *testing.T) {

func TestHostsHasDoesntFindMissingEntry(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}

if hosts.Has("10.0.0.7", "brada") {
@@ -35,7 +35,7 @@ func TestHostsHasDoesntFindMissingEntry(t *testing.T) {

func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 nada"),
NewHostsLine("10.0.0.7 yadda"),
@@ -50,14 +50,14 @@ func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
NewHostsLine("10.0.0.7 brada"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Add entry failed to append entry.")
}
}

func TestHostsAddWithComment(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 nada"),
}
@@ -71,14 +71,14 @@ func TestHostsAddWithComment(t *testing.T) {
NewHostsLine("10.0.0.7 yadda #Test Comment"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Add entry failed to append entry.")
}
}

func TestHostsAddWhenIpDoesntExist(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
}

@@ -90,21 +90,21 @@ func TestHostsAddWhenIpDoesntExist(t *testing.T) {
NewHostsLine("10.0.0.7 yadda"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Add entry failed to append entry.")
}
}

func TestHostsRemoveWhenLastHostIpCombo(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}

hosts.Remove("10.0.0.7", "nada")

expectedLines := []HostsLine{NewHostsLine("127.0.0.1 yadda")}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Remove entry failed to remove entry.")
}
}
@@ -112,7 +112,7 @@ func TestHostsRemoveWhenLastHostIpCombo(t *testing.T) {
func TestHostsRemoveWhenIpHasOtherHosts(t *testing.T) {
hosts := new(Hosts)

hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada brada")}

hosts.Remove("10.0.0.7", "nada")
@@ -120,18 +120,18 @@ func TestHostsRemoveWhenIpHasOtherHosts(t *testing.T) {
expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada")}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Remove entry failed to remove entry.")
}
}

func TestHostsRemoveMultipleEntries(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda nadda prada")}

hosts.Remove("127.0.0.1", "yadda", "prada")
if hosts.Lines[0].Raw != "127.0.0.1 nadda" {
if hosts.FileLines[0].Raw != "127.0.0.1 nadda" {
t.Error("Failed to remove multiple entries.")
}
}
@@ -139,7 +139,7 @@ func TestHostsRemoveMultipleEntries(t *testing.T) {
func TestHostsRemoveLineWithComments(t *testing.T) {
hosts := new(Hosts)

hosts.Lines = []HostsLine{
hosts.FileLines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("10.0.0.7 brada"),
}
@@ -147,7 +147,7 @@ func TestHostsRemoveLineWithComments(t *testing.T) {
nadaLine := NewHostsLine("10.0.0.7 nada")
nadaLine.Comment = "Test comment"

hosts.Lines = append(hosts.Lines, nadaLine)
hosts.FileLines = append(hosts.FileLines, nadaLine)

hosts.Remove("10.0.0.7", "nada")

@@ -156,7 +156,7 @@ func TestHostsRemoveLineWithComments(t *testing.T) {
NewHostsLine("10.0.0.7 brada"),
}

if !reflect.DeepEqual(hosts.Lines, expectedLines) {
if !reflect.DeepEqual(hosts.FileLines, expectedLines) {
t.Error("Remove entry failed to remove entry.")
}
}

Loading…
Cancel
Save