/
client.go
46 lines (37 loc) · 890 Bytes
/
client.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
package hostfile
import "github.com/txn2/txeh"
type HostfileInterface interface {
AddHost(ip, hostname string) error
RemoveHost(hostname string) error
}
// Hostfile represents the host file manager client
type Hostfile struct {
hosts *txeh.Hosts
}
// NewClient returns a new Hostfile manager client
func NewClient() (*Hostfile, error) {
hosts, err := txeh.NewHostsDefault()
if err != nil {
panic(err)
}
return &Hostfile{
hosts: hosts,
}, err
}
// AddHost adds a new host / ip entry into the hosts file
func (h *Hostfile) AddHost(ip, hostname string) error {
h.hosts.Reload()
h.hosts.AddHost(ip, hostname)
err := h.hosts.Save()
if err != nil {
return err
}
return nil
}
// RemoveHost removes a given hostname from the hosts file
func (h *Hostfile) RemoveHost(hostname string) error {
h.hosts.Reload()
h.hosts.RemoveHost(hostname)
h.hosts.Save()
return nil
}