Simple hosts file management in Go (golang).
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
Chris Wiegman 883358bf76
Update documentation
5ヶ月前
cmd add subcommand to remove entire section 5ヶ月前
.gitignore Ignore binary files. 4年前
.travis.yml Release 1.0.0 4年前
API.md Update documentation 5ヶ月前
LICENSE Added MIT license. 4年前
README.md Update documentation 5ヶ月前
const.go cleanup for tokenizing 5ヶ月前
const_windows.go cleanup for tokenizing 5ヶ月前
go.mod update version 5ヶ月前
go.sum cleanup for tokenizing 5ヶ月前
goodhosts.go Add field to separate the section from the rest 5ヶ月前
hostLine.go fix commenting on line addition and removal 5ヶ月前
hostLine_test.go fix existing tests for line comments 5ヶ月前
hostsFile.go remove print for debuggin 5ヶ月前
hostsFile_test.go fix existing tests 5ヶ月前
utils.go more test fixes for comments 5ヶ月前
utils_test.go fix existing tests 5ヶ月前

README.md

Goodhosts

Build Status

This is a fork of [https://github.com/lextoumbourou/goodhosts] as we needed a bit deeper management.

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

Features

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

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

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

Remove an section

$ goodhosts removesection sectionname

More

$ goodhosts --help

API Usage

Installation

$ go get github.com/ChrisWiegman/goodhosts

List entries

package main

import (
    "fmt"
    "github.com/ChrisWiegman/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/ChrisWiegman/goodhosts"
)

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

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

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

Add an entry

package main

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

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

Remove an entry

package main

import (
    "fmt"
    "github.com/ChrisWiegman/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

3.2 (2019-10-10)

  • Add ability to remove an entire section

3.1.1 (2019-10-10)

  • Fix existing tests

3.1 (2019-10-10)

  • Allow sectioning of IP addresses with “section name” in api
  • Various bugfixes

3.0.1 (2019-10-09)

  • Refactored with go mod support
  • Added ability to comment lines
  • Only one host per line for easier management

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