Goodhosts
Simple hosts file management in Go (golang).
A Surrealist Parisian Dinner Party chez Madame Rothschild, 1972
Command-Line Usage
List entries.
$ goodhosts list
127.0.0.1 localhost
10.0.0.5 my-home-server xbmc-server
10.0.0.6 my-desktop
Check for an entry.
$ goodhosts check 127.0.0.1 facebook
Add an entry.
$ goodhosts add 127.0.0.1 facebook
Remove an entry.
$ goodhosts remove 127.0.0.1 facebook
API Usage
List entries.
package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
h := hosts.NewHosts()
for _, line := range hosts.Lines {
fmt.Printf(line.Raw)
}
}
Check for an entry.
package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
h := hosts.NewHosts()
if h.HasEntry("127.0.0.1", "facebook") {
fmt.Println("Entry exists!")
}
}
Add an entry.
package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
h := hosts.NewHosts()
// Note that nothing will be added to the hosts file until ``Flush`` is called.
h.AddEntry("127.0.0.1", "facebook.com")
if err := h.Flush(); err != nil {
panic(err)
}
}
Remove an entry.
package main
import (
"fmt"
"github.com/lextoumbourou/goodhosts"
)
func main() {
h := hosts.NewHosts()
// Same deal, yo: call h.Flush() to make permanent.
h.RemoveEntry("127.0.0.1", "facebook")
if err := h.Flush(); err != nil {
panic(err)
}
}
Changelog
1.0.0 (2015-05-03)
License
MIT