txeh

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2019 License: Apache-2.0 Imports: 6 Imported by: 67

README

Etc Hosts Management Library

This small go library was developed to encapsulate the complexity of working with /etc/hosts by providing a simple interfact to load, add or remove rntries and save a /etc/host file. No validation is done on the input data and is considered out of scope for this project.

Basic implemention:


package main

import (
	"fmt"
	"strings"

	"github.com/txn2/txeh"
)

func main() {
	hosts := txeh.NewHostsDefault()

	hosts.AddHost("127.100.100.100", "test")
	hosts.AddHost("127.100.100.101", "logstash")
	hosts.AddHosts("127.100.100.102", []string{"a", "b", "c"})
	
	hosts.RemoveHosts([]string{"example", "example.machine", "example.machine.example.com"})
	hosts.RemoveHosts(strings.Fields("example2 example.machine2 example.machine.example.com2"))

	
	hosts.RemoveAddress("127.1.27.1")
	
	removeList := []string{
		"127.1.27.15",
		"127.1.27.14",
		"127.1.27.13",
	}
	
	hosts.RemoveAddresses(removeList)
	
	hfData := hosts.RenderHostsFile()

	fmt.Println(hfData)
	
	hosts.SaveAs("./test.hosts")
}

Documentation

Index

Constants

View Source
const ADDRESS = 30
View Source
const COMMENT = 20
View Source
const EMPTY = 10
View Source
const UNKNOWN = 0

Variables

This section is empty.

Functions

This section is empty.

Types

type AddressLocations

type AddressLocations map[string]int

AddressLocations the location of an address in the HFL

type HostFileLine

type HostFileLine struct {
	OriginalLineNum int
	LineType        int
	Address         string
	Parts           []string
	Hostnames       []string
	Raw             string
	Trimed          string
}

HostFileLine

func ParseHosts

func ParseHosts(path string) []HostFileLine

ParseHosts

type HostFileLines

type HostFileLines []HostFileLine

HostFileLines

type HostLocations

type HostLocations map[string]int

HostLocations maps a hostname to an original line number

type Hosts

type Hosts struct {
	sync.Mutex
	*HostsConfig
	// contains filtered or unexported fields
}

Hosts

func NewHosts

func NewHosts(hc *HostsConfig) *Hosts

NewHosts returns a new hosts object

func NewHostsDefault

func NewHostsDefault() *Hosts

NewHostsDefault returns a hosts object with default configuration

func (*Hosts) AddHost

func (h *Hosts) AddHost(addressRaw string, hostRaw string)

AddHost adds a host to an address and removes the host from any existing address is may be associated with

func (*Hosts) AddHosts

func (h *Hosts) AddHosts(address string, hosts []string)

AddHosts adds an array of hosts to the first matching address it finds or creates the address and adds the hosts

func (*Hosts) GetHostFileLines

func (h *Hosts) GetHostFileLines() *HostFileLines

GetHostFileLines

func (*Hosts) HostAddressLookup

func (h *Hosts) HostAddressLookup(host string) (bool, string, int)

HostAddressLookup returns true is the host is found, a string containing the address and the index of the hfl

func (*Hosts) Reload

func (h *Hosts) Reload()

Reload hosts file

func (*Hosts) RemoveAddress

func (h *Hosts) RemoveAddress(address string)

RemoveAddress removes all entries (lines) with the provided address.

func (*Hosts) RemoveAddresses

func (h *Hosts) RemoveAddresses(addresses []string)

RemoveAddresses removes all entries (lines) with the provided address.

func (*Hosts) RemoveFirstAddress

func (h *Hosts) RemoveFirstAddress(address string) bool

RemoveFirstAddress removed the first entry (line) found with the provided address.

func (*Hosts) RemoveFirstHost

func (h *Hosts) RemoveFirstHost(host string) bool

RemoveHost the first hostname entry found and returns true if successful

func (*Hosts) RemoveHost

func (h *Hosts) RemoveHost(host string)

RemoveHost removes all hostname entries of provided host

func (*Hosts) RemoveHosts

func (h *Hosts) RemoveHosts(hosts []string)

RemoveHosts removes all hostname entries of the provided host slice

func (*Hosts) RenderHostsFile

func (h *Hosts) RenderHostsFile() string

RenderHostsFile

func (*Hosts) Save

func (h *Hosts) Save() error

Save rendered hosts file

func (*Hosts) SaveAs

func (h *Hosts) SaveAs(fileName string) error

SaveAs saves rendered hosts file to the filename specified

type HostsConfig

type HostsConfig struct {
	ReadFilePath  string
	WriteFilePath string
}

HostsConfig

Jump to

Keyboard shortcuts

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