HostsButler is a command-line utility, written in the D programming language, designed to simplify the management and manipulation of your hosts file. It supports hosts files with entries grouped into named blocks that you can easily enable or disable as needed.
Managing large or frequently changing hosts files can be a headache. HostsButler addresses common pain points by providing:
- Intelligent Parsing & Manipulation: HostsButler accurately reads and processes entries from your hosts file.
- Block Management: Define and manage named blocks of host entries, allowing you to activate or deactivate them as a whole. This is perfect for switching between development, testing, or production environments, or for quickly enabling/disabling ad-blocking lists.
- Smart Sorting: Organizes your host entries logically. Domain-based hosts are sorted by their Top-Level Domain (TLD) for intuitive readability.
- Automatic Deduplication: Keeps your hosts file lean and efficient by identifying and removing duplicate entries.
- Cross-Platform Compatibility: Built with portability in mind, HostsButler provides Makefiles for both POSIX-like systems (Linux, macOS) and Windows, ensuring a consistent build experience.
To compile HostsButler from its source code, follow these steps:
You'll need a D compiler and make installed on your system:
- D Compiler: Any D compiler will work, but LDC is recommended for its performance and compatibility. Alternatively, you can use DMD or GDC.
make: This build automation tool is essential. On Windows,makeis typically available through environments like MinGW/MSYS2 or via package managers like Chocolatey.
-
Clone the repository:
git clone https://github.com/ragnarokkr/hostsbutler.git cd hostsbutler -
Build:
# Build a release version (recommended for general use) make # Build a debug version (useful for development and troubleshooting) make debug
ℹ️ Important for Windows Users: When running
makecommand on Windows, ensure you use the executable corresponding to your installed toolchain (e.g.,make.exefor MSYS/Cygwin,mingw32-make.exefor MinGW).
HostsButler offers a straightforward command-line interface.
USAGE: hostsbutler [options] [hosts_file]
Filters and enables/disables hosts entries.
Options:
-s, --sort Sorts host entries.
-u, --unique Removes duplicate entries (implies --sort).
-e, --enable <block_id> Enables a specified block of entries.
-d, --disable <block_id> Disables a specified block of entries.
-v, --version Displays the program version.
-h, --help Displays this help information.Key Usage Notes:
- Option Grouping: Options can be grouped for brevity (e.g.,
-suis equivalent to-s -u). - Multiple Blocks: You can specify multiple blocks to enable or disable by repeating the
-eor-doptions (e.g.,-e Block1 -e Block2). - Input Source: If no
hosts_fileis specified, HostsButler will read input directly fromstdin, allowing for piping operations.
Here are some common scenarios demonstrating HostsButler's capabilities:
# Sort your hosts file entries
hostsbutler -s path/to/your/hosts_file
# Remove duplicate entries from a hosts file (output to stdout)
cat path/to/your/hosts_file | hostsbutler -u
# Enable specific blocks (e.g., 'Development' and 'AdBlock')
hostsbutler -e Development -e AdBlock path/to/your/hosts_file
# Disable particular blocks (e.g., 'OldConfig' and 'Legacy')
hostsbutler -d OldConfig -d Legacy path/to/your/hosts_file
# Combine multiple operations: remove duplicates, sort, enable 'Staging', and disable 'Production'
cat path/to/your/hosts_file | hostsbutler -su -e Staging -d Production > cleaned_hosts_fileNow, for those of you who insist on knowing exactly how fast this rocket-powered hamster wheel spins (and possibly want to prove your machine is superior to your neighbor's toaster), you can use hyperfine:
hyperfine -N -w 10000 './build/release/hostsbutler -su -d Web -e Test tests/hosts.example >/dev/null'
# Linux:
Benchmark 1: ./build/release/hostsbutler -su -d Web -e Test tests/hosts.example >/dev/null
Time (mean ± σ): 4.9 ms ± 0.8 ms [User: 3.1 ms, System: 0.7 ms]
Range (min … max): 4.0 ms … 8.8 ms 604 runs
# Windows Sucks Tales™ - unexpectedly slower for some reason:
Benchmark 1: ./build/release/hostsbutler -su -d Web -e Test tests/hosts.example
Time (mean ± σ): 21.3 ms ± 1.7 ms [User: 8.4 ms, System: 7.8 ms]
Range (min … max): 19.1 ms … 27.8 ms 130 runsThis project is an exploration of the D language, undertaken in my spare time. Consequently, updates may not be frequent. However, contributions are encouraged; please refer to CONTRIBUTING.md for detailed information.
HostsButler is open-source software released under the MIT License.