A simple CLI tool to manage and switch between multiple hosts file profiles for different development environments
npm install @milkmaccya2/hostswitch

A simple CLI tool for switching hosts file profiles
HostSwitch is a CLI tool that makes it easy to switch between different hosts configurations for development and testing environments. Unlike GUI applications like Gas Mask, it focuses on quick command-line operations.
- šØāš» Web developers working with multiple development environments
- š§ Engineers who need to test across local, staging, and production
- š¢ System administrators managing multiple server environments
- š Anyone who prefers fast CLI operations
- ā
Multiple hosts profile management - For development, staging, production, etc.
- š¾ Automatic backup - Saves current hosts before switching
- šØ Colorful output - Clear status visibility
- ā” Simple CLI - Easy-to-remember commands
- š Safe operations - Explicit sudo requirements
- šÆ Interactive mode - User-friendly prompts when run without arguments
- Node.js 20.0.0 or higher
- macOS / Linux / Windows (WSL recommended)
- sudo permissions (for hosts file switching)
bash
Global installation
npm install -g @milkmaccya2/hostswitchOr run directly with npx
npx @milkmaccya2/hostswitch list
`$3
`bash
Clone repository
git clone https://github.com/milkmaccya2/hostswitch.git
cd hostswitchInstall dependencies
npm installBuild TypeScript source
npm run buildGlobal installation (optional)
npm link
`Usage
$3
`bash
Run without arguments for interactive mode
hostswitchNavigate with arrow keys, select options with Enter
? What would you like to do? (Use arrow keys)
⯠Switch profile (current: local)
List all profiles
Create new profile
Edit profile
Delete profile
Exit
`$3
`bash
hostswitch list
or
hostswitch ls
`$3
`bash
Create with default content
hostswitch create developmentCreate from current hosts file
hostswitch create production --from-current
`$3
`bash
No need to type sudo manually - it will automatically rerun with sudo if needed
hostswitch switch development
or
hostswitch use development
`$3
`bash
hostswitch show development
or
hostswitch cat development
`$3
`bash
hostswitch edit development
`$3
`bash
Delete with confirmation flag (required for safety)
hostswitch delete development --force
or
hostswitch rm development --force
`
Common Use Cases
$3
`bash
For local development
hostswitch create local
hostswitch edit local
127.0.0.1 api.myapp.local
127.0.0.1 app.myapp.local
For Docker environment
hostswitch create docker
hostswitch edit docker
172.17.0.2 api.myapp.docker
172.17.0.3 db.myapp.docker
Switch between them (auto-sudo)
hostswitch switch local
`$3
`bash
Reference team member's environment
hostswitch create team-dev --from-currentSwitch back to your environment (auto-sudo)
hostswitch switch local
`$3
`bash
Create hosts pointing to production
hostswitch create production
hostswitch edit production
192.168.1.100 api.myapp.com
192.168.1.101 app.myapp.com
Run tests (auto-sudo)
hostswitch switch production
After testing (auto-sudo)
hostswitch switch local
`Development
$3
`bash
Install dependencies
npm installBuild TypeScript
npm run buildWatch mode for development
npm run build:watchRun in development mode
npm run dev -- list
`$3
The project uses Biome for linting and formatting:`bash
Check for linting errors
npm run lintAuto-fix linting and formatting issues
npm run lint:fixFormat code only
npm run formatCheck formatting without fixing
npm run format:checkRun linting + formatting + tests (recommended for CI)
npm run check
`$3
`bash
Run tests in watch mode
npm testRun tests once
npm run test:runOpen test UI
npm run test:uiGenerate coverage report
npm run test:coverage
`$3
`
hostswitch/
āāā src/
ā āāā interfaces/ # Type definitions and abstractions
ā āāā core/ # Domain logic (business rules)
ā āāā cli/ # CLI-specific implementation
ā āāā infrastructure/ # External dependencies implementation
ā āāā config/ # Configuration management
ā āāā hostswitch.ts # Entry point with dependency injection
āāā dist/ # Compiled JavaScript (generated)
āāā tsconfig.json # TypeScript configuration
āāā package.json
`$3
The application follows a clean architecture pattern:
- Domain Layer: Pure business logic in
HostSwitchService
- CLI Layer: Command handling and user interaction
- Infrastructure Layer: File system, logging, and process execution
- Dependency Injection: All dependencies are injected via interfacesThis design enables:
- Easy unit testing with mocked dependencies
- Clear separation of concerns
- Platform-agnostic core logic
- Future extensibility (e.g., GUI or API interfaces)
Troubleshooting
$3
#### Auto-sudo Detection
HostSwitch automatically detects when sudo privileges are needed and will prompt for admin access:
`bash
ā
Recommended - HostSwitch handles sudo automatically
hostswitch switch dev
ā "Requesting administrative access..." (auto-sudo prompt)
ā
Manual sudo also works
sudo hostswitch switch devā Will show auto-sudo prompt if permissions needed
hostswitch switch dev # Auto-prompts for sudo
`#### Permission Denied Errors
`bash
If auto-sudo fails, try manual sudo
sudo hostswitch switch devOn Windows, run as Administrator
Right-click Command Prompt ā "Run as administrator"
`$3
#### Profile Not Found
`bash
Check available profiles
hostswitch listVerify profile name (case-sensitive)
hostswitch show [profile-name]Check profile directory
ls ~/.hostswitch/profiles/
`#### Profile Corruption
`bash
Recreate corrupted profile
hostswitch delete corrupted-profile --force
hostswitch create corrupted-profile --from-current
`$3
For Windows, we recommend using WSL (Windows Subsystem for Linux). If using native Windows, run Command Prompt as Administrator.
Note: Native Windows support has been improved. The tool now automatically detects the Windows hosts file location (
C:\Windows\System32\drivers\etc\hosts).Data Storage
- Profiles:
~/.hostswitch/profiles/
- Backups: ~/.hostswitch/backups/
- Current profile info: ~/.hostswitch/current.json`MIT License - See LICENSE file for details.
Bug reports and feature requests are welcome at GitHub Issues.