This is a fork of [https://github.com/lextoumbourou/goodhosts] as I desired more organization of hosts entries than the original could provide.
Simple hosts file (/etc/hosts
) management in Go (golang).
- List, add, remove and check hosts file entries from code or the command-line.
- Windows support.
$ goodhosts list
127.0.0.1 localhost
10.0.0.5 my-home-server xbmc-server
10.0.0.6 my-desktop
Add --all
flag to include comments.
$ goodhosts check 127.0.0.1 hiroy.club
$ goodhosts add 127.0.0.1 hiroy.club
Or entries.
$ goodhosts add 127.0.0.1 hiroy.club gitea.io notochrome.org
$ goodhosts rm 127.0.0.1 hiroy.club
Or entries.
$ goodhosts rm 127.0.0.1 hiroy.club gitea.io notochrome.org
$ goodhosts removesection --section=sectionname
$ goodhosts --help
$ go get github.com/ChrisWiegman/goodhosts/v4
package main
import (
"fmt"
"github.com/ChrisWiegman/goodhosts/v4/pkg/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts("")
for _, line := range hosts.Lines {
fmt.Println(line.Raw)
}
}
package main
import (
"fmt"
"github.com/ChrisWiegman/goodhosts/v4/pkg/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts("")
if hosts.Has("127.0.0.1", "hiroy.club", true) {
fmt.Println("Entry exists!")
return
}
fmt.Println("Entry doesn't exist!")
}
package main
import (
"fmt"
"github.com/ChrisWiegman/goodhosts/v4/pkg/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts("")
// Note that nothing will be added to the hosts file until ``hosts.Flush`` is called.
hosts.Add("127.0.0.1", "This is a line comment", "hiroy.club", "notochrome.org")
if err := hosts.Flush(); err != nil {
panic(err)
}
}
package main
import (
"fmt"
"github.com/ChrisWiegman/goodhosts/v4/pkg/goodhosts"
)
func main() {
hosts := goodhosts.NewHosts("")
// Same deal, yo: call hosts.Flush() to make permanent.
hosts.Remove("127.0.0.1", "hiroy.club", "notochrome.org")
if err := hosts.Flush(); err != nil {
panic(err)
}
}
- Complete refactor
- Now uses proper gitea domain
- Use Cobra for command line items
- Implement all section features in CLI
- Cleanup API
- Add ability to remove an entire section
- Fix existing tests
- Allow sectioning of IP addresses with "section name" in api
- Various bugfixes
- Refactored with go mod support
- Added ability to comment lines
- Only one host per line for easier management
- Added Windows support.
- Added command-line docs.
- Breaking API change.
- Add support for adding and removing multiple hosts.
- Added
--all
flag. - Handle malformed IP addresses.
- Initial release.