Documentation
¶
Overview ¶
Package txeh provides /etc/hosts file management capabilities. It offers a simple interface for adding, removing, and querying hostname-to-IP mappings with thread-safe operations and cross-platform support (Linux, macOS, Windows).
Index ¶
- Constants
- type AddressLocations
- type HostFileLine
- type HostFileLines
- type HostLocations
- type Hosts
- func (h *Hosts) AddHost(addressRaw string, hostRaw string)
- func (h *Hosts) AddHostWithComment(addressRaw string, hostRaw string, comment string)
- func (h *Hosts) AddHosts(address string, hosts []string)
- func (h *Hosts) AddHostsWithComment(address string, hosts []string, comment string)
- func (h *Hosts) GetHostFileLines() HostFileLines
- func (h *Hosts) HostAddressLookup(host string, ipFamily IPFamily) (bool, string, int)
- func (h *Hosts) ListAddressesByHost(hostname string, exact bool) [][]string
- func (h *Hosts) ListHostsByCIDR(cidr string) [][]string
- func (h *Hosts) ListHostsByComment(comment string) []string
- func (h *Hosts) ListHostsByIP(address string) []string
- func (h *Hosts) Reload() error
- func (h *Hosts) RemoveAddress(address string)
- func (h *Hosts) RemoveAddresses(addresses []string)
- func (h *Hosts) RemoveByComment(comment string)
- func (h *Hosts) RemoveByComments(comments []string)
- func (h *Hosts) RemoveCIDRs(cidrs []string) error
- func (h *Hosts) RemoveFirstAddress(address string) bool
- func (h *Hosts) RemoveFirstHost(host string) bool
- func (h *Hosts) RemoveHost(host string)
- func (h *Hosts) RemoveHosts(hosts []string)
- func (h *Hosts) RenderHostsFile() string
- func (h *Hosts) Save() error
- func (h *Hosts) SaveAs(fileName string) error
- type HostsConfig
- type IPFamily
Constants ¶
const ( UNKNOWN = 0 // Unknown line type. EMPTY = 10 // Empty line. COMMENT = 20 // Comment line starting with #. ADDRESS = 30 // Address line with IP and hostnames. )
Line type constants for HostFileLine.
const DefaultMaxHostsPerLineWindows = 9
DefaultMaxHostsPerLineWindows is the default maximum number of hostnames per line on Windows. Windows has a limitation where lines with more than ~9 hostnames may not resolve correctly.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AddressLocations ¶
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
Trimmed string
Comment string
}
HostFileLine represents a single line in a hosts file.
func ParseHosts ¶
func ParseHosts(path string) ([]HostFileLine, error)
ParseHosts reads and parses a hosts file from the given path.
func ParseHostsFromString ¶ added in v1.5.3
func ParseHostsFromString(input string) ([]HostFileLine, error)
ParseHostsFromString parses hosts file content from a string.
type HostFileLines ¶
type HostFileLines []HostFileLine
HostFileLines is a slice of HostFileLine entries.
type HostLocations ¶
HostLocations maps a hostname to an original line number
type Hosts ¶
type Hosts struct {
sync.Mutex
*HostsConfig
// contains filtered or unexported fields
}
Hosts represents a parsed hosts file with thread-safe operations.
func NewHostsDefault ¶
NewHostsDefault returns a hosts object with default configuration
func (*Hosts) AddHost ¶
AddHost adds a host to an address and removes the host from any existing address is may be associated with
func (*Hosts) AddHostWithComment ¶ added in v1.7.0
AddHostWithComment adds a host to an address with an inline comment. The comment will appear after the hostnames on the line (e.g., "127.0.0.1 host # comment"). If the address already exists with the same comment, the host is appended to that line (respecting MaxHostsPerLine). If the address exists with a different comment, a new line is created with the specified comment.
func (*Hosts) AddHosts ¶
AddHosts adds an array of hosts to the first matching address it finds or creates the address and adds the hosts
func (*Hosts) AddHostsWithComment ¶ added in v1.7.0
AddHostsWithComment adds an array of hosts to an address with a comment. All hosts will share the same comment. If the address already exists with the same comment, hosts are appended to that line (respecting MaxHostsPerLine). If the address exists with a different comment, a new line is created.
func (*Hosts) GetHostFileLines ¶
func (h *Hosts) GetHostFileLines() HostFileLines
GetHostFileLines returns a copy of all parsed host file lines.
func (*Hosts) HostAddressLookup ¶
HostAddressLookup returns true if the host is found, the address string, and the index of the host file line. This is part of the public API for consumers that need direct address lookups by IP family.
func (*Hosts) ListAddressesByHost ¶ added in v1.5.2
ListAddressesByHost returns a list of IPs associated with a given hostname
func (*Hosts) ListHostsByCIDR ¶ added in v1.5.2
ListHostsByCIDR returns a list of IPs and hostnames associated with a given CIDR
func (*Hosts) ListHostsByComment ¶ added in v1.7.0
ListHostsByComment returns all hostnames on lines with the given comment
func (*Hosts) ListHostsByIP ¶ added in v1.5.2
ListHostsByIP returns a list of hostnames associated with a given IP address
func (*Hosts) Reload ¶
Reload re-reads the hosts file from disk and replaces the in-memory state. This is part of the public API for consumers who manage long-lived Hosts instances.
func (*Hosts) RemoveAddress ¶
RemoveAddress removes all entries (lines) with the provided address.
func (*Hosts) RemoveAddresses ¶
RemoveAddresses removes all entries (lines) with the provided address.
func (*Hosts) RemoveByComment ¶ added in v1.7.0
RemoveByComment removes all host entries that have the specified comment. This removes entire lines where the comment matches.
func (*Hosts) RemoveByComments ¶ added in v1.7.0
RemoveByComments removes all host entries that have any of the specified comments. This removes entire lines where the comment matches. This is part of the public API for bulk comment-based cleanup (e.g., kubefwd teardown).
func (*Hosts) RemoveCIDRs ¶ added in v1.5.4
RemoveCIDRs Remove CIDR Range (Classless inter-domain routing) examples:
127.1.0.0/16 = 127.1.0.0 -> 127.1.255.255 127.1.27.0/24 = 127.1.27.0 -> 127.1.27.255
func (*Hosts) RemoveFirstAddress ¶
RemoveFirstAddress removes the first entry (line) found with the provided address.
func (*Hosts) RemoveFirstHost ¶
RemoveFirstHost the first hostname entry found and returns true if successful
func (*Hosts) RemoveHost ¶
RemoveHost removes all hostname entries of provided host
func (*Hosts) RemoveHosts ¶
RemoveHosts removes all hostname entries of the provided host slice
func (*Hosts) RenderHostsFile ¶
RenderHostsFile returns the hosts file content as a formatted string.
type HostsConfig ¶
type HostsConfig struct {
ReadFilePath string
WriteFilePath string
// RawText for input. If RawText is set ReadFilePath, WriteFilePath are ignored. Use RenderHostsFile rather
// than save to get the results.
RawText *string
// MaxHostsPerLine limits the number of hostnames per line when adding hosts.
// This is useful for Windows which has a limitation of ~9 hostnames per line.
// Values:
// 0 = auto-detect (Windows: 9, others: unlimited)
// -1 = force unlimited (no limit)
// >0 = explicit limit
MaxHostsPerLine int
}
HostsConfig contains configuration for reading and writing hosts files.
