goodhosts

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2015 License: MIT Imports: 4 Imported by: 0

README

Goodhosts

Simple hosts file management in Go (golang).


A Surrealist Parisian Dinner Party chez Madame Rothschild, 1972

Build Status

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)
    }
}
API Docs

Changelog

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

Return a new instance of “Hosts“.

func (*Hosts) AddEntry

func (h *Hosts) AddEntry(ip string, host string)

Add an entry to the hosts file.

func (Hosts) Flush

func (h Hosts) Flush() error

Flush any changes made to hosts file.

func (Hosts) HasEntry

func (h Hosts) HasEntry(ip string, host string) (bool, error)

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

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) RemoveEntry

func (h *Hosts) RemoveEntry(ip string, host string) error

Remove an entry from the hosts file.

type HostsLine

type HostsLine struct {
	Ip    string
	Hosts []string
	Raw   string
}

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