hosts

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package hosts provides a backend interface for /etc/hosts file management.

Index

Constants

This section is empty.

Variables

View Source
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 NewClient

func NewClient() *Client

NewClient creates a new hosts Client.

func (*Client) GenerateManualInstructions

func (c *Client) GenerateManualInstructions(config HostsConfig) string

GenerateManualInstructions generates instructions for manually updating /etc/hosts.

func (*Client) Remove

func (c *Client) Remove(_ context.Context, hostnames []string) error

Remove removes hostname entries from /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.

Directories

Path Synopsis
Package hosts provides auto-generated mocks for the hosts.Backend interface.
Package hosts provides auto-generated mocks for the hosts.Backend interface.

Jump to

Keyboard shortcuts

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