Просмотр исходного кода

comments now work on individual lines

master
Chris Wiegman 10 месяцев назад
Родитель
Сommit
74f0634237
Подписано: chriswiegman Идентификатор GPG ключа: 2B23D7A74FDE2DF4
5 измененных файлов: 43 добавлений и 23 удалений
  1. +3
    -3
      cmd/main.go
  2. +1
    -1
      goodhosts_test.go
  3. +18
    -13
      hostLine.go
  4. +10
    -5
      hostsFile.go
  5. +11
    -1
      utils.go

+ 3
- 3
cmd/main.go Просмотреть файл

@ -40,7 +40,7 @@ Options:
for _, line := range hosts.Lines {
var lineOutput string
if line.IsComment() && !args["--all"].(bool) {
if goodhosts.IsComment(line.Raw) && !args["--all"].(bool) {
continue
}
@ -48,7 +48,7 @@ Options:
if line.Err != nil {
lineOutput = fmt.Sprintf("%s # <<< Malformated!", lineOutput)
}
total += 1
total++
fmt.Println(lineOutput)
}
@ -87,7 +87,7 @@ Options:
os.Exit(1)
}
err = hosts.Add(ip, hostEntries...)
err = hosts.Add(ip, "", hostEntries...)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err.Error()))
os.Exit(2)

+ 1
- 1
goodhosts_test.go Просмотреть файл

@ -9,7 +9,7 @@ import (
func TestHostsLineIsComment(t *testing.T) {
comment := " # This is a comment "
line := NewHostsLine(comment)
result := line.IsComment()
result := IsComment(line.Raw)
if !result {
t.Error(fmt.Sprintf("'%s' should be a comment", comment))
}

+ 18
- 13
hostLine.go Просмотреть файл

@ -8,17 +8,11 @@ import (
// HostsLine Represents a single line in the hosts file.
type HostsLine struct {
IP string
Hosts []string
Raw string
Err error
}
// IsComment Return ```true``` if the line is a comment.
func (l HostsLine) IsComment() bool {
trimLine := strings.TrimSpace(l.Raw)
isComment := strings.HasPrefix(trimLine, commentChar)
return isComment
IP string
Hosts []string
comment string
Raw string
Err error
}
// NewHostsLine Return a new instance of ```HostsLine```.
@ -30,14 +24,25 @@ func NewHostsLine(raw string) HostsLine {
}
output := HostsLine{Raw: raw}
if !output.IsComment() {
if !IsComment(output.Raw) {
rawIP := fields[0]
if net.ParseIP(rawIP) == nil {
output.Err = fmt.Errorf("Bad hosts line: %q", raw)
}
output.IP = rawIP
output.Hosts = fields[1:]
var outputFields []string
for i, field := range fields {
if IsComment(field) {
output.comment = field[i:]
break
}
outputFields = append(outputFields, field)
}
output.Hosts = outputFields[1:]
}
return output

+ 10
- 5
hostsFile.go Просмотреть файл

@ -64,7 +64,11 @@ func (h Hosts) Flush() error {
w := bufio.NewWriter(file)
for _, line := range h.Lines {
fmt.Fprintf(w, "%s%s", line.Raw, eol)
var comment string
if len(line.comment) > 0 {
comment = " #" + line.comment
}
fmt.Fprintf(w, "%s%s%s", line.Raw, comment, eol)
}
err = w.Flush()
@ -76,7 +80,7 @@ func (h Hosts) Flush() error {
}
// Add an entry to the hosts file.
func (h *Hosts) Add(ip string, hosts ...string) error {
func (h *Hosts) Add(ip, comment string, hosts ...string) error {
if net.ParseIP(ip) == nil {
return fmt.Errorf("%q is an invalid IP address", ip)
@ -85,6 +89,7 @@ func (h *Hosts) Add(ip string, hosts ...string) error {
for _, host := range hosts {
endLine := NewHostsLine(buildRawLine(ip, host))
endLine.comment = comment
h.Lines = append(h.Lines, endLine)
}
@ -109,7 +114,7 @@ func (h *Hosts) Remove(ip string, hosts ...string) error {
for _, line := range h.Lines {
// Bad lines or comments just get readded.
if line.Err != nil || line.IsComment() || line.IP != ip {
if line.Err != nil || IsComment(line.Raw) || line.IP != ip {
outputLines = append(outputLines, line)
continue
}
@ -141,7 +146,7 @@ func (h Hosts) getHostPosition(ip string, host string) int {
for i := range h.Lines {
line := h.Lines[i]
if !line.IsComment() && line.Raw != "" {
if !IsComment(line.Raw) && line.Raw != "" {
if ip == line.IP && itemInSlice(host, line.Hosts) {
return i
}
@ -154,7 +159,7 @@ 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]
if !line.IsComment() && line.Raw != "" {
if !IsComment(line.Raw) && line.Raw != "" {
if line.IP == ip {
return i
}

+ 11
- 1
utils.go Просмотреть файл

@ -1,6 +1,9 @@
package goodhosts
import "fmt"
import (
"fmt"
"strings"
)
func itemInSlice(item string, list []string) bool {
for _, i := range list {
@ -18,3 +21,10 @@ func buildRawLine(ip string, host string) string {
return output
}
// IsComment Return ```true``` if the line is a comment.
func IsComment(line string) bool {
trimLine := strings.TrimSpace(line)
isComment := strings.HasPrefix(trimLine, commentChar)
return isComment
}

Загрузка…
Отмена
Сохранить