Documentation ¶
Index ¶
- Variables
- func Add(c *cli.Context)
- func Apply(c *cli.Context)
- func Del(c *cli.Context)
- func Dump(c *cli.Context)
- func Fix(c *cli.Context)
- func GetHostsPath() string
- func Has(c *cli.Context)
- func LooksLikeIPv4(ip string) bool
- func LooksLikeIPv6(ip string) bool
- func Ls(c *cli.Context)
- func MakeSurrogateIP(IP net.IP) net.IP
- func MaybeError(c *cli.Context, message string)
- func MaybeErrorln(c *cli.Context, message string)
- func MaybePrintln(c *cli.Context, message string)
- func MaybeSaveHostFile(c *cli.Context, hostfile *Hostfile)
- func OnOff(c *cli.Context)
- func StrPadRight(s string, l int) string
- func TrimWS(s string) string
- type Hostfile
- type Hostlist
- func (h *Hostlist) Add(hostnamev *Hostname) error
- func (h *Hostlist) Apply(jsonbytes []byte) error
- func (h *Hostlist) Contains(b *Hostname) bool
- func (h *Hostlist) ContainsDomain(domain string) bool
- func (h *Hostlist) ContainsIP(IP net.IP) bool
- func (h *Hostlist) Disable(domain string) bool
- func (h *Hostlist) DisableV(domain string, version int) bool
- func (h *Hostlist) Dump() ([]byte, error)
- func (h *Hostlist) Enable(domain string) bool
- func (h *Hostlist) EnableV(domain string, version int) bool
- func (h *Hostlist) FilterByDomain(domain string) (hostnames []*Hostname)
- func (h *Hostlist) FilterByDomainV(domain string, version int) (hostnames []*Hostname)
- func (h *Hostlist) FilterByIP(IP net.IP) (hostnames []*Hostname)
- func (h *Hostlist) Format() []byte
- func (h *Hostlist) GetUniqueIPs() []net.IP
- func (h *Hostlist) IndexOf(host *Hostname) int
- func (h *Hostlist) IndexOfDomainV(domain string, version int) int
- func (h Hostlist) Len() int
- func (h Hostlist) Less(A, B int) bool
- func (h *Hostlist) Remove(index int) int
- func (h *Hostlist) RemoveDomain(domain string) int
- func (h *Hostlist) RemoveDomainV(domain string, version int) int
- func (h *Hostlist) Sort()
- func (h Hostlist) Swap(i, j int)
- type Hostname
Constants ¶
This section is empty.
Variables ¶
var ErrCantWriteHostFile = fmt.Errorf( "Unable to write to %s. Maybe you need to sudo?", GetHostsPath())
ErrCantWriteHostFile indicates that we are unable to write to the hosts file
var ErrInvalidVersionArg = errors.New("Version argument must be 4 or 6")
ErrInvalidVersionArg is raised when a function expects IPv 4 or 6 but is passed a value not 4 or 6.
Functions ¶
func Add ¶
Add command parses <hostname> <ip> and adds or updates a hostname in the hosts file. If the aff command is used the hostname will be disabled or added in the off state.
func GetHostsPath ¶
func GetHostsPath() string
GetHostsPath returns the location of the hostfile; either env HOSTESS_PATH or /etc/hosts if HOSTESS_PATH is not set.
func LooksLikeIPv4 ¶
LooksLikeIPv4 returns true if the IP looks like it's IPv4. This does not validate whether the string is a valid IP address.
func LooksLikeIPv6 ¶
LooksLikeIPv6 returns true if the IP looks like it's IPv6. This does not validate whether the string is a valid IP address.
func MakeSurrogateIP ¶
MakeSurrogateIP takes an IP like 127.0.0.1 and munges it to 0.0.0.1 so we can sort it more easily. Note that we don't actually want to change the value, so we use value copies here (not pointers).
func MaybeError ¶
MaybeError will print an error message unless -s is passed and then exit
func MaybeErrorln ¶
MaybeErrorln will print an error message unless -s is passed
func MaybePrintln ¶
MaybePrintln will print a message unless -q or -s is passed
func MaybeSaveHostFile ¶
MaybeSaveHostFile will output or write the Hostfile, or exit 1 and error.
func OnOff ¶
OnOff enables (uncomments) or disables (comments) the specified hostname in the hosts file. Exits code 1 if the hostname is missing.
func StrPadRight ¶
StrPadRight adds spaces to the right of a string until it reaches l length. If the input string is already that long, do nothing.
Types ¶
type Hostfile ¶
Hostfile represents /etc/hosts (or a similar file, depending on OS), and includes a list of Hostnames. Hostfile includes
func AlwaysLoadHostFile ¶
AlwaysLoadHostFile will load, parse, and return a Hostfile. If we encouter errors they will be printed to the terminal, but we'll try to continue.
func LoadHostfile ¶
LoadHostfile creates a new Hostfile struct and tries to populate it from disk. Read and/or parse errors are returned as a slice.
func MaybeLoadHostFile ¶
MaybeLoadHostFile will try to load, parse, and return a Hostfile. If we encounter errors we will terminate, unless -f is passed.
func NewHostfile ¶
func NewHostfile() *Hostfile
NewHostfile creates a new Hostfile object from the specified file.
func (*Hostfile) Format ¶
Format takes the current list of Hostnames in this Hostfile and turns it into a string suitable for use as an /etc/hosts file. Sorting uses the following logic: 1. List is sorted by IP address 2. Commented items are left in place 3. 127.* appears at the top of the list (so boot resolvers don't break) 4. When present, localhost will always appear first in the domain list
type Hostlist ¶
type Hostlist []*Hostname
Hostlist is a sortable set of Hostnames. When in a Hostlist, Hostnames must follow some rules:
- Hostlist may contain IPv4 AND IPv6 ("IP version" or "IPv") Hostnames.
- Names are only allowed to overlap if IP version is different.
- Adding a Hostname for an existing name will replace the old one.
The Hostlist uses a deterministic Sort order designed to make a hostfile output look a particular way. Generally you don't need to worry about this as Sort will be called automatically before Format. However, the Hostlist may or may not be sorted at any particular time during runtime.
See the docs and implementation in Sort and Add for more details.
func ParseLine ¶
ParseLine parses an individual line in a hostfile, which may contain one (un)commented ip and one or more hostnames. For example
127.0.0.1 localhost mysite1 mysite2
func (*Hostlist) Add ¶
Add a new Hostname to this hostlist. Add uses some merging logic in the event it finds duplicated hostnames. In the case of a conflict (incompatible entries) the last write wins. In the case of duplicates, duplicates will be removed and the remaining entry will be enabled if any of the duplicates was enabled.
Both duplicate and conflicts return errors so you are aware of them, but you don't necessarily need to do anything about the error.
func (*Hostlist) ContainsDomain ¶
ContainsDomain returns true if a Hostname in this Hostlist matches domain
func (*Hostlist) ContainsIP ¶
ContainsIP returns true if a Hostname in this Hostlist matches IP
func (*Hostlist) DisableV ¶
DisableV will change any Hostnames matching domain and IP version to be disabled.
This function will panic if IP version is not 4 or 6.
func (*Hostlist) EnableV ¶
EnableV will change a Hostname matching domain and IP version to be enabled.
This function will panic if IP version is not 4 or 6.
func (*Hostlist) FilterByDomain ¶
FilterByDomain filters the list of hostnames by Domain.
func (*Hostlist) FilterByDomainV ¶
FilterByDomainV filters the list of hostnames by domain and IPv4 or IPv6. This should never contain more than one item, but returns a list for consistency with other filter functions.
This function will panic if IP version is not 4 or 6.
func (*Hostlist) FilterByIP ¶
FilterByIP filters the list of hostnames by IP address.
func (*Hostlist) Format ¶
Format takes the current list of Hostnames in this Hostfile and turns it into a string suitable for use as an /etc/hosts file. Sorting uses the following logic:
1. List is sorted by IP address 2. Commented items are sorted displayed 3. 127.* appears at the top of the list (so boot resolvers don't break) 4. When present, "localhost" will always appear first in the domain list
func (*Hostlist) GetUniqueIPs ¶
GetUniqueIPs extracts an ordered list of unique IPs from the Hostlist. This calls Sort() internally.
func (*Hostlist) IndexOf ¶
IndexOf will indicate the index of a Hostname in Hostlist, or -1 if it is not found.
func (*Hostlist) IndexOfDomainV ¶
IndexOfDomainV will indicate the index of a Hostname in Hostlist that has the same domain and IP version, or -1 if it is not found.
This function will panic if IP version is not 4 or 6.
func (*Hostlist) Remove ¶
Remove will delete the Hostname at the specified index. If index is out of bounds (i.e. -1), Remove silently no-ops. Remove returns the number of items removed (0 or 1).
func (*Hostlist) RemoveDomain ¶
RemoveDomain removes both IPv4 and IPv6 Hostname entries matching domain. Returns the number of entries removed.
func (*Hostlist) RemoveDomainV ¶
RemoveDomainV removes a Hostname entry matching the domain and IP version.
type Hostname ¶
type Hostname struct { Domain string `json:"domain"` IP net.IP `json:"ip"` Enabled bool `json:"enabled"` IPv6 bool `json:"-"` }
Hostname represents a hosts file entry, including a Domain, IP, whether the Hostname is enabled (uncommented in the hosts file), and whether the IP is in the IPv6 format. You should always create these with NewHostname(). Note: when using Hostnames in the context of a Hostlist, you should not change the Hostname fields except through the Hostlist's aggregate methods. Doing so can cause unexpected behavior. Instead, use Hostlist's Add, Remove, Enable, and Disable methods.
func NewHostname ¶
NewHostname creates a new Hostname struct and automatically sets the IPv6 field based on the IP you pass in.
func (*Hostname) Equal ¶
Equal compares two Hostnames. Note that only the Domain and IP fields are compared because Enabled is transient state, and IPv6 should be set automatically based on IP.
func (*Hostname) Format ¶
Format outputs the Hostname as you'd see it in a hosts file, with a comment if it is disabled. E.g. # 127.0.0.1 blah.example.com
func (*Hostname) FormatEnabled ¶
FormatEnabled displays Hostname.Enabled as (On) or (Off)
func (*Hostname) FormatHuman ¶
FormatHuman outputs the Hostname in a more human-readable format: blah.example.com -> 127.0.0.1 (Off)