goodhosts

package module
v2.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2015 License: MIT Imports: 7 Imported by: 0

README

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

Download the binary and put it in your path.

$ wget https://github.com/lextoumbourou/goodhosts/releases/download/v2.0.0/goodhosts
$ chmod +x goodhosts
$ export PATH=$(pwd):$PATH
$ goodhosts --help
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
Add an entry
$ goodhosts add 127.0.0.1 facebook

Or entries.

$ goodhosts add 127.0.0.1 facebook twitter gmail
Remove an entry
$ goodhosts rm 127.0.0.1 facebook

Or entries.

$ goodhosts rm 127.0.0.1 facebook twitter gmail
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") {
        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")

    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", "twitter")

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

Changelog

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


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hosts

type Hosts struct {
	Path  string
	Lines []HostsLine
}

Represents a hosts file.

func NewHosts

func NewHosts() (Hosts, error)

Return a new instance of “Hosts“.

func (*Hosts) Add

func (h *Hosts) Add(ip string, hosts ...string) error

Add an entry to the hosts file.

func (Hosts) Flush

func (h Hosts) Flush() error

Flush any changes made to hosts file.

func (Hosts) Has

func (h Hosts) Has(ip string, host string) bool

Return a bool if ip/host combo in hosts file.

func (*Hosts) IsWritable

func (h *Hosts) IsWritable() bool

Return ```true``` if hosts file is writable.

func (*Hosts) Load

func (h *Hosts) Load() error

Load the hosts file into ```l.Lines```. ```Load()``` is called by ```NewHosts()``` and ```Hosts.Flush()``` so you generally you won't need to call this yourself.

func (*Hosts) Remove

func (h *Hosts) Remove(ip string, hosts ...string) error

Remove an entry from the hosts file.

type HostsLine

type HostsLine struct {
	IP    string
	Hosts []string
	Raw   string
	Err   error
}

Represents a single line in the hosts file.

func NewHostsLine

func NewHostsLine(raw string) HostsLine

Return a new instance of ```HostsLine```.

func (HostsLine) IsComment

func (l HostsLine) IsComment() bool

Return ```true``` if the line is a comment.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL