Simple hosts file management in Go (golang).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Chris Wiegman 74f0634237
comments now work on individual lines
10 months ago
cmd comments now work on individual lines 10 months ago
.gitignore Ignore binary files. 5 years ago
.travis.yml Release 1.0.0 5 years ago
API.md Don't export constants. 5 years ago
LICENSE Added MIT license. 5 years ago
README.md Make examples a lil' less useless. 4 years ago
const.go cleanup for tokenizing 10 months ago
const_windows.go cleanup for tokenizing 10 months ago
go.mod cleanup for tokenizing 10 months ago
go.sum cleanup for tokenizing 10 months ago
goodhosts.go cleanup for tokenizing 10 months ago
goodhosts_test.go comments now work on individual lines 10 months ago
hostLine.go comments now work on individual lines 10 months ago
hostsFile.go comments now work on individual lines 10 months ago
utils.go comments now work on individual lines 10 months ago
utils_test.go fix existing tests 10 months ago

README.md

Goodhosts

Simple hosts file (/etc/hosts) management in Go (golang).


A Surrealist Parisian Dinner Party chez Madame Rothschild, 1972

Build Status

Features

  • List, add, remove and check hosts file entries from code or the command-line.
  • Windows support.

Command-Line Usage

Installation

Linux

Download the binary and put it in your path.

$ wget -O goodhosts https://github.com/lextoumbourou/goodhosts/releases/download/v2.1.0/goodhosts-linux
$ chmod +x goodhosts
$ export PATH=$(pwd):$PATH
$ goodhosts --help

Windows

Download the binary and do Windowsy stuff with it (doc PR welcome).

List entries

$ 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.

Check for an entry

$ goodhosts check 127.0.0.1 facebook.com

Add an entry

$ goodhosts add 127.0.0.1 facebook.com

Or entries.

$ goodhosts add 127.0.0.1 facebook.com twitter.com gmail.com

Remove an entry

$ goodhosts rm 127.0.0.1 facebook.com

Or entries.

$ goodhosts rm 127.0.0.1 facebook.com twitter.com gmail.com

More

$ goodhosts --help

API Usage

Installation

$ go get github.com/lextoumbourou/goodhosts

List entries

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    for _, line := range hosts.Lines {
        fmt.Println(line.Raw)
    }
}

Check for an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    if hosts.Has("127.0.0.1", "facebook.com") {
        fmt.Println("Entry exists!")
        return
    }

    fmt.Println("Entry doesn't exist!")
}

Add an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/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", "facebook.com", "twitter.com")

    if err := hosts.Flush(); err != nil {
        panic(err)
    }
}

Remove an entry

package main

import (
    "fmt"
    "github.com/lextoumbourou/goodhosts"
)

func main() {
    hosts := goodhosts.NewHosts()

    // Same deal, yo: call hosts.Flush() to make permanent.
    hosts.Remove("127.0.0.1", "facebook.com", "twitter.com")

    if err := hosts.Flush(); err != nil {
        panic(err)
    }
}

More

Changelog

2.1.0 (2015-06-08)

  • Added Windows support.
  • Added command-line docs.

2.0.0 (2015-05-04)

  • Breaking API change.
  • Add support for adding and removing multiple hosts.
  • Added --all flag.
  • Handle malformed IP addresses.

1.0.0 (2015-05-03)

  • Initial release.

License

MIT