hosts

package module
Version: v0.0.0-...-ec3898e Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: MIT Imports: 5 Imported by: 0

README

= A library to interact with etc/hosts file

**Work in Progress**

This go library provides methods to interact with `etc/hosts` file.
One can parse the `etc/hosts` file to a go struct.
One can parse a go struct to a valid well-formed `etc/hosts` file.

== Some examples

_For more examples check the tests._

=== Parse from io.Reader

[source,go]
----
package main

import (
    "fmt"
    "github.com/bitofcode/hosts/parser"
    "os"
)
import "bytes"

func main() {

	reader := bytes.NewBufferString(
		`127.0.0.1  localhost localhost.local
# a comment
192.168.10.10 example.com
192.168.10.10 example.io # another comment
192.168.15.15 hello.world# a dummy host`)

	entrySet, err := parser.Read(reader)

    if err != nil {
    	fmt.Fprintf(os.Stderr, "unexpected error %v\n", err)
    }

    fmt.Println(entrySet)

}
----

=== Parse to a io.Writer

[source,go]
----
package main

import (
    "bytes"
    "fmt"
    "github.com/bitofcode/hosts"
    "github.com/bitofcode/hosts/parser"
    "os"
)


func main() {
    entrySet := hosts.NewEntrySet()
    entrySet.AddEntry(hosts.NewEntry("127.0.0.1", []string{"localhost", "localhost.local"}))
    entrySet.AddEntry(hosts.NewEntry("10.0.0.10", []string{"example.com", "example.io"}))
    entrySet.AddEntry(hosts.NewEntry("127.0.0.1", []string{"docker.host.local"}))

    buffer := bytes.NewBuffer(make([]byte, 0))

    err := parser.Write(entrySet, buffer)

    if err != nil {
        fmt.Fprintf(os.Stderr,"unexpected error '%v'\n", err)
    }
}
----

Documentation

Overview

Package hosts provides an implementation to model an /etc/hosts file.

Index

Constants

This section is empty.

Variables

View Source
var ErrorInvalidHostName = errors.New("invalid host-name")
View Source
var ErrorInvalidIp = errors.New("invalid ip")
View Source
var ErrorNilEntry = errors.New("entry is nil")

Functions

This section is empty.

Types

type Entry

type Entry interface {
	Ip() net.IP
	IpString() string
	HostNames() []string
	AddHostName(hostName string) error
	String() string
	Contains(hostName string) bool
}

An Entry represent a line in /etc/hosts with multiple hosts associate to one ip.

func CloneEntry

func CloneEntry(entry Entry) (Entry, error)

func NewEntry

func NewEntry(ip net.IP, hostNames []string) (Entry, error)

NewEntry returns an Entry which contains the given ip and the host names.

func NewEntryIp

func NewEntryIp(ip net.IP) (Entry, error)

NewEntryIp returns an Entry which contains the given ip.

func NewEntryUnsafe

func NewEntryUnsafe(ip net.IP, hosts []string) Entry

type EntrySet

type EntrySet interface {
	AddEntry(entry Entry, entries ...Entry)
	Contains(entry Entry) bool
	EntriesOfIP(ip net.IP) (hosts []string, ok bool)
	AllEntries() []Entry
}

func NewEntrySet

func NewEntrySet() EntrySet

Directories

Path Synopsis
Package hostsfile provides an interface to interact with etc/hosts-file.
Package hostsfile provides an interface to interact with etc/hosts-file.
Package parser provides an interface to parse line/file-entry from/to hosts-file.
Package parser provides an interface to parse line/file-entry from/to hosts-file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL