Skip to content

Commit

Permalink
housekeeping, fixed typos and deprications
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Jul 23, 2023
1 parent ca3571a commit 171fcae
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions txeh.go
Expand Up @@ -2,8 +2,8 @@ package txeh

import (
"fmt"
"io/ioutil"
"net"
"os"
"regexp"
"runtime"
"strings"
Expand All @@ -22,13 +22,11 @@ const (
IPFamilyV6
)

// HostsConfig
type HostsConfig struct {
ReadFilePath string
WriteFilePath string
}

// Hosts
type Hosts struct {
sync.Mutex
*HostsConfig
Expand All @@ -42,18 +40,16 @@ type AddressLocations map[string]int
// to an original line number
type HostLocations map[string]int

// HostFileLines
type HostFileLines []HostFileLine

// HostFileLine
type HostFileLine struct {
OriginalLineNum int
LineType int
Address string
Parts []string
Hostnames []string
Raw string
Trimed string
Trimmed string
Comment string
}

Expand Down Expand Up @@ -105,7 +101,7 @@ func (h *Hosts) SaveAs(fileName string) error {
h.Lock()
defer h.Unlock()

err := ioutil.WriteFile(fileName, hfData, 0644)
err := os.WriteFile(fileName, hfData, 0644)
if err != nil {
return err
}
Expand Down Expand Up @@ -175,7 +171,7 @@ func (h *Hosts) RemoveHost(host string) {
}
}

// RemoveHost the first hostname entry found and returns true if successful
// RemoveFirstHost the first hostname entry found and returns true if successful
func (h *Hosts) RemoveFirstHost(host string) bool {
h.Lock()
defer h.Unlock()
Expand Down Expand Up @@ -228,7 +224,7 @@ func (h *Hosts) AddHost(addressRaw string, hostRaw string) {

// if the hostname is at a different address, go and remove it from the address
for hidx, hst := range h.hostFileLines[hflIdx].Hostnames {
//for localhost we can match more than one host
// for localhost, we can match more than one host
if isLocalhost(address) {
break
}
Expand Down Expand Up @@ -295,7 +291,6 @@ func (h *Hosts) HostAddressLookup(host string, ipFamily IPFamily) (bool, string,
return false, "", 0
}

// RenderHostsFile
func (h *Hosts) RenderHostsFile() string {
h.Lock()
defer h.Unlock()
Expand All @@ -309,17 +304,15 @@ func (h *Hosts) RenderHostsFile() string {
return hf
}

// GetHostFileLines
func (h *Hosts) GetHostFileLines() *HostFileLines {
h.Lock()
defer h.Unlock()

return &h.hostFileLines
}

// ParseHosts
func ParseHosts(path string) ([]HostFileLine, error) {
input, err := ioutil.ReadFile(path)
input, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -332,33 +325,33 @@ func ParseHosts(path string) ([]HostFileLine, error) {

hostFileLines := make([]HostFileLine, len(dataLines))

// trim leading an trailing whitespace
// trim leading and trailing whitespace
for i, l := range dataLines {
curLine := &hostFileLines[i]
curLine.OriginalLineNum = i
curLine.Raw = l

// trim line
curLine.Trimed = strings.TrimSpace(l)
curLine.Trimmed = strings.TrimSpace(l)

// check for comment
if strings.HasPrefix(curLine.Trimed, "#") {
if strings.HasPrefix(curLine.Trimmed, "#") {
curLine.LineType = COMMENT
continue
}

if curLine.Trimed == "" {
if curLine.Trimmed == "" {
curLine.LineType = EMPTY
continue
}

curLineSplit := strings.SplitN(curLine.Trimed, "#", 2)
curLineSplit := strings.SplitN(curLine.Trimmed, "#", 2)
if len(curLineSplit) > 1 {
curLine.Comment = curLineSplit[1]
}
curLine.Trimed = curLineSplit[0]
curLine.Trimmed = curLineSplit[0]

curLine.Parts = strings.Fields(curLine.Trimed)
curLine.Parts = strings.Fields(curLine.Trimmed)

if len(curLine.Parts) > 1 {
curLine.LineType = ADDRESS
Expand Down

0 comments on commit 171fcae

Please sign in to comment.