-
Notifications
You must be signed in to change notification settings - Fork 12
/
hostsline.go
126 lines (100 loc) · 2.87 KB
/
hostsline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package hostsfile
import (
"fmt"
"net"
"sort"
"strings"
)
// HostsLine represents a line of the hosts file after being parsed into their respective parts
type HostsLine struct {
IP string // IP found at the beginning of the line
Hosts []string // Hosts split into a slice on the space char
Comment string // Contents of everything after the comment char in the line
Raw string // Raw contents of the line as parsed in or updated after changes
Err error // Used for error checking during parsing
}
const commentChar string = "#"
// NewHostsLine takes a raw line as a string and parses it into a new instance of HostsLine e.g. "192.168.1.1 host1 host2 # comments"
func NewHostsLine(raw string) HostsLine {
output := HostsLine{Raw: raw}
if output.HasComment() { //trailing comment
commentSplit := strings.Split(output.Raw, commentChar)
raw = commentSplit[0]
output.Comment = commentSplit[1]
}
if output.IsComment() { //whole line is comment
return output
}
fields := strings.Fields(raw)
if len(fields) == 0 {
return output
}
rawIP := fields[0]
if net.ParseIP(rawIP) == nil {
output.Err = fmt.Errorf("bad hosts line: %q", raw)
}
output.IP = rawIP
output.Hosts = fields[1:]
return output
}
// String to make HostsLine a fmt.Stringer
func (l *HostsLine) String() string {
return l.ToRaw()
}
// ToRaw returns the HostsLine's contents as a raw string
func (l *HostsLine) ToRaw() string {
var comment string
if l.IsComment() { //Whole line is comment
return l.Raw
}
if l.Comment != "" {
comment = fmt.Sprintf(" %s%s", commentChar, l.Comment)
}
return fmt.Sprintf("%s %s%s", l.IP, strings.Join(l.Hosts, " "), comment)
}
// RemoveDuplicateHosts checks all hosts in a line and removes duplicates
func (l *HostsLine) RemoveDuplicateHosts() {
unique := make(map[string]struct{})
hosts := make([]string, len(l.Hosts))
copy(hosts, l.Hosts)
l.Hosts = []string{}
for _, host := range hosts {
if _, ok := unique[host]; !ok {
unique[host] = struct{}{}
l.Hosts = append(l.Hosts, host)
}
}
l.RegenRaw()
}
// Deprecated: will be made internal, combines the hosts and comments of two lines together,
func (l *HostsLine) Combine(hostline HostsLine) {
l.combine(hostline)
}
func (l *HostsLine) combine(hostline HostsLine) {
l.Hosts = append(l.Hosts, hostline.Hosts...)
if l.Comment == "" {
l.Comment = hostline.Comment
} else {
l.Comment = fmt.Sprintf("%s %s", l.Comment, hostline.Comment)
}
l.RegenRaw()
}
func (l *HostsLine) SortHosts() {
sort.Strings(l.Hosts)
l.RegenRaw()
}
func (l *HostsLine) IsComment() bool {
return strings.HasPrefix(strings.TrimSpace(l.Raw), commentChar)
}
func (l *HostsLine) HasComment() bool {
return strings.Contains(l.Raw, commentChar)
}
func (l *HostsLine) IsValid() bool {
return l.IP != ""
}
func (l *HostsLine) IsMalformed() bool {
return l.Err != nil
}
func (l *HostsLine) RegenRaw() {
l.Raw = l.ToRaw()
}