hostess

repository·master·Indexed 21 days ago

https://github.com/cbednarski/hostess

An idempotent command-line utility for managing system hosts files (/etc/hosts on Unix/Linux/macOS and C:\Windows\System32\drivers\etc\hosts on Windows). It provides a CLI and Go API to add, remove, enable, disable, and list hostnames, as well as tools to format the hosts file and export/import configurations via JSON.

Tokens
2.3K
Snippets
13
Records
15
Agent score
74%

What's inside hostess

  1. Understand hosts file formatting behavior

    master

    hostess adapts its writing style based on the operating system or the HOSTESS_FMT variable:

    Unix Format

    Follows the standard man hosts format where multiple hostnames can share a single line per IP address:

    127.0.0.1 localhost hostname2 hostname3
    127.0.1.1 machine.name
    # 10.10.20.30 some.host

    Windows Format

    Writes each hostname on its own individual line:

    127.0.0.1 localhost
    127.0.0.1 hostname2
    127.0.0.1 hostname3
  2. Use hostess to manage hosts files

    master

    hostess is an idempotent command-line utility for managing your /etc/hosts (Unix) or C:\Windows\System32\drivers\etc\hosts (Windows) file.

    Permissions Note: Because the hosts file is a protected system file, you must run hostess with elevated privileges:

    • Unix/Linux/macOS: Use sudo or run as root.
    • Windows: Run from an elevated prompt (Right-click and select Run as administrator).

    Common commands include adding entries for specific hostnames and IP addresses.

    hostess add local.example.com 127.0.0.1
    hostess add staging.example.com 10.0.2.16
  3. Install hostess

    master

    You can install hostess by downloading a precompiled release from GitHub or by building it from source using Go. To build from source, clone the repository and use make install.

    git clone https://github.com/cbednarski/hostess
    cd hostess
    make install
  4. Configure hostess via environment variables

    master

    You can override the default platform detection and file locations using the following environment variables:

    • HOSTESS_FMT: Set to windows or unix to force a specific hosts file format.
      • unix format: One line per IP address (e.g., 127.0.0.1 host1 host2).
      • windows format: Each hostname on its own line (e.g., 127.0.0.1 host1\n127.0.0.1 host2).
    • HOSTESS_PATH: Set this to override the default location of the hosts file.
      • Default Unix: /etc/hosts
      • Default Windows: C:\Windows\System32\drivers\etc\hosts
  5. Manage hosts files with hostess commands

    master

    The commands.go file provides a high-level API for managing a system's hosts file. It wraps the core hostess library to provide common operations like adding, removing, enabling, disabling, and listing hostnames. Most functions accept an *Options struct which can control whether changes are actually written to disk or just previewed via stdout.

    Key Operations

    • Add/Update: Adds a new hostname/IP pair or updates an existing one.
    • Remove: Deletes hostnames matching a specific domain.
    • Enable/Disable: Toggles the active status of a hostname (commenting/uncommenting in the file).
    • Has: Checks for the existence of a hostname (exits with code 1 if not found).
    • List: Displays all hostnames, IPs, and their status in a formatted list.
    • Format: Removes duplicates and cleans up the hosts file.
    • Dump/Apply: Exports the current hosts to JSON or applies host configurations from a JSON file.
    // Example: Adding a host
    err := Add(&Options{Preview: false}, "example.com", "127.0.0.1")
    
    // Example: Checking if a host exists
    err := Has(&Options{Preview: false}, "example.com")
  6. Configure command execution with Options

    master

    The Options struct is used to control the behavior of the command functions, specifically regarding how changes are persisted.

    FieldTypeDescription
    PreviewboolIf true, the command will not write changes to the hosts file. Instead, it will output the resulting formatted hosts file to stdout. This is useful for dry-runs.
    type Options struct {
    	Preview bool
    }
  7. Enable or disable a hostname

    master

    These functions toggle the active status of a hostname in the hosts file (typically by commenting or uncommenting the line).

    Enable

    Activates a hostname.

    • err := Enable(options, "hostname.com")

    Disable

    Deactivates a hostname.

    • err := Disable(options, "hostname.com")
    • Note: If the hostname does not exist, Disable will print an error to stderr but will not return an error, as the desired state (the host being disabled/absent) is already achieved.
    // Enable a host
    err := Enable(options, "example.com")
    
    // Disable a host
    err := Disable(options, "example.com")
  8. Remove a hostname from the hosts file

    master

    The Remove function deletes any hostname matching the provided domain from the hosts file.

    Parameters:

    • options *Options: Configuration.
    • hostname string: The domain name to remove.

    Returns:

    • error: Returns an error if loading the hostfile fails or if saving/previewing fails.
    err := Remove(options, "oldhost.local")
  9. Add or update a hostname

    master

    The Add function parses a hostname and an IP address, then adds or updates that entry in the hosts file. If the domain already exists, the entry is updated. The operation is idempotent.

    Parameters:

    • options *Options: Configuration (use Preview: true for a dry-run).
    • hostname string: The domain name to add.
    • ip string: The IP address to associate with the domain.

    Returns:

    • error: Returns an error if the hostname creation fails or if saving/previewing fails.
    err := Add(options, "myhost.local", "10.0.0.1")
  10. Check if a hostname exists

    master

    The Has function checks if a specific hostname is present in the hosts file.

    Behavior:

    • If the hostname is found, it prints a success message and returns nil.
    • If the hostname is not found, it prints a message and calls os.Exit(1). This makes it suitable for use in shell scripts to verify presence.

    Parameters:

    • options *Options: Configuration.
    • hostname string: The domain name to check.

    Returns:

    • error: Returns an error if loading the hostfile fails.
    // In a script, this will exit with 1 if not found
    err := Has(options, "exists.com")
  11. Format the hosts file

    master

    The Format function cleans up the hosts file by removing duplicate entries and resolving conflicts. If the file is already formatted and contains no duplicates, the function performs no action.

    Parameters:

    • options *Options: Configuration.

    Returns:

    • error: Returns an error if loading or saving fails.
    err := Format(options)
  12. Apply host configurations from a JSON file

    master

    The Apply function reads a JSON file containing host configurations and applies them to the current hosts file.

    Parameters:

    • options *Options: Configuration.
    • filename string: The path to the JSON file containing the host data.

    Returns:

    • error: Returns an error if the file cannot be read, if the JSON is invalid, or if applying the changes fails.
    err := Apply(options, "hosts_backup.json")