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")