Documentation
¶
Overview ¶
Package hosts provides a backend interface for /etc/hosts file management.
Index ¶
- Variables
- type Backend
- type Client
- func (c *Client) GenerateManualInstructions(config HostsConfig) string
- func (c *Client) Remove(_ context.Context, hostnames []string) error
- func (c *Client) Update(_ context.Context, config HostsConfig) error
- func (c *Client) Validate(_ context.Context, config HostsConfig) ([]ValidationResult, error)
- type HostsConfig
- type ValidationResult
- type ValidationState
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRequiresRoot is returned when an operation requires root privileges. ErrRequiresRoot = errors.New("requires root privileges (use sudo)") )
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface {
// Validate checks if hostnames in /etc/hosts match the expected configuration.
// Returns a slice of ValidationResults for any hostnames that are missing or incorrect.
// Empty slice means all hostnames are valid.
Validate(ctx context.Context, config HostsConfig) ([]ValidationResult, error)
// Update adds or updates hostname entries in /etc/hosts.
// Requires root privileges.
Update(ctx context.Context, config HostsConfig) error
// Remove removes hostname entries from /etc/hosts.
// Requires root privileges.
Remove(ctx context.Context, hostnames []string) error
// GenerateManualInstructions generates instructions for manually updating /etc/hosts.
GenerateManualInstructions(config HostsConfig) string
}
Backend defines the interface for /etc/hosts file management.
type Client ¶
type Client struct{}
Client implements the Backend interface using txn2/txeh library.
func (*Client) GenerateManualInstructions ¶
func (c *Client) GenerateManualInstructions(config HostsConfig) string
GenerateManualInstructions generates instructions for manually updating /etc/hosts.
func (*Client) Update ¶
func (c *Client) Update(_ context.Context, config HostsConfig) error
Update adds or updates hostname entries in /etc/hosts.
func (*Client) Validate ¶
func (c *Client) Validate(_ context.Context, config HostsConfig) ([]ValidationResult, error)
Validate checks if hostnames in /etc/hosts match the expected configuration.
type HostsConfig ¶
type HostsConfig struct {
IPAddress string // IP address to map hostnames to
Hostnames []string // Hostnames to manage
}
HostsConfig contains configuration for hosts file operations.
type ValidationResult ¶
type ValidationResult struct {
Hostname string // Hostname being validated
Expected string // Expected IP address
Current string // Current IP address (empty if missing)
State ValidationState // Validation state
}
ValidationResult represents the validation result for a single hostname.
type ValidationState ¶
type ValidationState int
ValidationState represents the state of a hostname in /etc/hosts.
const ( // Valid indicates hostname exists with correct IP. Valid ValidationState = iota // Missing indicates hostname is not in /etc/hosts. Missing // Incorrect indicates hostname exists but with wrong IP. Incorrect )
func (ValidationState) String ¶
func (s ValidationState) String() string
String returns string representation of ValidationState.