hostcraft-cli 2.2.0

CLI for managing your system hosts file
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# hostcraft

[![Crates.io](https://img.shields.io/crates/v/hostcraft-cli)](https://crates.io/crates/hostcraft-cli)
[![GitHub Release](https://img.shields.io/github/v/release/Zaberahmed/hostcraft?filter=cli-v*&label=release)](https://github.com/Zaberahmed/hostcraft/releases?q=cli-v)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey)](#installation)

<p align="center">
  <img src="../assets/cli-demo.png" alt="hostcraft CLI in action" width="700" />
</p>

A fast, cross-platform CLI for managing your system hosts file — add, remove, toggle, edit, and list host entries directly from your terminal without ever manually editing the file.

---

## Ecosystem

| Component | Description | Status | Link |
|---|---|---|---|
| `hostcraft-core` | Shared library | ✅ Published | [crates.io]https://crates.io/crates/hostcraft-core |
| `hostcraft-cli` | Terminal interface (this crate) | ✅ Published | [crates.io]https://crates.io/crates/hostcraft-cli and [Releases]https://github.com/Zaberahmed/hostcraft/releases/tag/cli-v2.1.1 |
| `hostcraft-gui` | Desktop GUI (Tauri) | 🟡 Beta | [Releases]https://github.com/Zaberahmed/hostcraft/releases |

---

## Features

- **List** all host entries with colour-coded active/inactive status
- **Add** a new entry with a hostname and IP address
- **Edit** an existing entry's hostname, IP, or both in a single command
- **Remove** entries by exact name by default, with explicit `--partial` opt-in for bulk matching
- **Toggle** entries on/off without deleting them, with explicit `--partial` opt-in
- **Update** to the latest version with a single command — `hostcraft update`
- Background update checks — once every 24 hours, a notice is printed at the end of any command if a newer version is available
- Entries are never silently lost — disabled entries are preserved as commented-out lines
- Duplicate entry detection — adding the same hostname + IP combination is rejected
- IPv4 and IPv6 support
- Cross-platform — works on macOS, Linux, and Windows
- Coloured, aligned terminal output powered by `anstyle`
- Friendly error messages with platform-specific permission hints

---

## Installation

### macOS / Linux

```sh
curl -fsSL https://raw.githubusercontent.com/Zaberahmed/hostcraft/main/install.sh | sh
```

### Windows (PowerShell)

```powershell
irm https://raw.githubusercontent.com/Zaberahmed/hostcraft/main/install.ps1 | iex
```

### Manual download

Download the archive for your platform from the [latest release](https://github.com/Zaberahmed/hostcraft/releases/tag/cli-v2.1.1), extract it, and move the binary to any directory on your `PATH`.

| Platform | File |
|---|---|
| macOS (Apple Silicon + Intel) | `hostcraft-universal-apple-darwin.tar.gz` |
| Linux x86_64 | `hostcraft-x86_64-unknown-linux-gnu.tar.gz` |
| Linux ARM64 | `hostcraft-aarch64-unknown-linux-gnu.tar.gz` |
| Windows x86_64 | `hostcraft-x86_64-pc-windows-msvc.zip` |

### Via Cargo (Rust users)

```sh
cargo install hostcraft-cli
```

This compiles the binary from source and places it in `~/.cargo/bin/`, which is on your `PATH` by default after a standard Rust installation.

### Verify

```sh
hostcraft --version
```

---

## Uninstall

### macOS / Linux

```sh
curl -fsSL https://raw.githubusercontent.com/Zaberahmed/hostcraft/main/uninstall.sh | sh
```

### Windows (PowerShell)

```powershell
irm https://raw.githubusercontent.com/Zaberahamed/hostcraft/main/uninstall.ps1 | iex
```

The script removes the binary, cleans up the cache directory, and removes the install location from your `PATH`.

### Via Cargo

```sh
cargo uninstall hostcraft-cli
```

---

## Quick Start

```sh
# See all entries in your hosts file
hostcraft list

# Add a new entry
sudo hostcraft add myapp.local 127.0.0.1

# Edit an existing entry
sudo hostcraft edit myapp.local --new-ip 192.168.1.50

# Disable it temporarily without removing it
sudo hostcraft toggle myapp.local

# Remove it entirely
sudo hostcraft remove myapp.local

# Update to the latest version
hostcraft update
```

> **Windows users:** run your terminal as Administrator instead of using `sudo`.

---

## Commands

### `list`

Prints all entries in your hosts file with colour-coded status.

```sh
hostcraft list
```

**Output:**

```
  127.0.0.1            localhost                      ● Active
  255.255.255.255      broadcasthost                  ● Active
  ::1                  localhost                      ● Active
  127.0.0.1            myapp.local                    ○ Inactive
```

- `` green — entry is active and in effect
- `` red/dimmed — entry is inactive (commented out in the hosts file)

---

### `add <hostname> <ip>`

Adds a new active entry. The entry is immediately written to the hosts file.

```sh
sudo hostcraft add myapp.local 127.0.0.1
sudo hostcraft add staging.myapp.com 192.168.1.50
sudo hostcraft add mysite.local ::1          # IPv6
```

Adding a duplicate (same hostname **and** same IP) is rejected with an error — the file is left unchanged.

---

### `edit <hostname>`

Edits an existing entry's IP address, hostname, or both in a single command. Requires an exact hostname match.

```sh
# Change only the IP
sudo hostcraft edit myapp.local --new-ip 192.168.1.50

# Change only the hostname
sudo hostcraft edit myapp.local --new-name myapp.dev

# Change both at once
sudo hostcraft edit myapp.local --new-ip 192.168.1.50 --new-name myapp.dev
```

At least one of `--new-ip` or `--new-name` must be provided. Returns an error if no entry is found with that exact hostname, or if the supplied values are identical to the current ones.

---

### `remove <name>`

Removes entries with an exact hostname match by default.

```sh
# Remove an exact hostname
sudo hostcraft remove myapp.local

# Remove all entries matching a substring (explicit opt-in)
sudo hostcraft remove myapp --partial      # removes myapp.local, myapp.dev, etc.
```

Returns an error if no matching entry is found. In `--partial` mode, success output includes how many entries were removed.

---

### `toggle <name>`

Flips entries between active and inactive without removing them. Useful for temporarily disabling a host without losing it.

```sh
sudo hostcraft toggle myapp.local

# Before:   127.0.0.1 myapp.local
# After:  # 127.0.0.1 myapp.local

sudo hostcraft toggle myapp.local

# Before: # 127.0.0.1 myapp.local
# After:    127.0.0.1 myapp.local

# Toggle all entries matching a substring (explicit opt-in)
sudo hostcraft toggle myapp --partial
```

In `--partial` mode, success output includes how many entries were toggled.

---

### `update`

Checks GitHub for a newer version and updates the binary in place if one is available.

```sh
hostcraft update
```

If already on the latest version:

```
✓ hostcraft is up to date (v2.2.0)
```

If a newer version is found, it downloads the pre-built binary for your platform, replaces the current executable, and confirms:

```
↑ Updating v2.1.1 → v2.2.0 ...
✓ Updated to v2.2.0
```

> **Note:** On macOS/Linux, if hostcraft was installed to a root-owned location like `/usr/local/bin`, run `sudo hostcraft update`.

> **Passive notices:** hostcraft also checks for updates silently in the background once every 24 hours. If a newer version is found, a notice is printed at the end of whatever command you ran:
>
> ```
> ↑ Update available: v2.1.1 → v2.2.0
>   Run `hostcraft update` to install.
> ```
>
> The check runs in a background thread and does not add any latency to your command.

---

## Options

| Flag | Default | Description |
|---|---|---|
| `--file <path>` | Platform default (see below) | Override the hosts file path |
| `--help` || Print help for the command or subcommand |
| `--version` || Print the installed version |

Command-specific flags:

- `remove --partial` — remove all entries whose hostname contains the provided pattern
- `toggle --partial` — toggle all entries whose hostname contains the provided pattern

### Default hosts file path

| Platform | Path |
|---|---|
| macOS / Linux | `/etc/hosts` |
| Windows | `C:\Windows\System32\drivers\etc\hosts` |

### Overriding the path

The `--file` flag is useful for testing against a copy without touching your real hosts file:

```sh
hostcraft --file ./hosts-copy list
hostcraft --file ./hosts-copy add myapp.local 127.0.0.1
```

> **Note:** `--file` must come **before** the subcommand.
>
> ```sh
> ✅  hostcraft --file ./hosts-copy list
> ❌  hostcraft list --file ./hosts-copy
> ```

---

## Permissions

Writing to the system hosts file requires elevated privileges on all platforms.

### macOS / Linux

Prefix write commands with `sudo`:

```sh
sudo hostcraft add myapp.local 127.0.0.1
sudo hostcraft edit myapp.local --new-ip 192.168.1.50
sudo hostcraft remove myapp.local
sudo hostcraft toggle myapp.local
```

`list` does **not** require `sudo`:

```sh
hostcraft list
```

### Windows

Run your terminal (Command Prompt or PowerShell) **as Administrator**, then use `hostcraft` normally without any prefix:

```sh
hostcraft add myapp.local 127.0.0.1
hostcraft edit myapp.local --new-ip 192.168.1.50
hostcraft remove myapp.local
hostcraft toggle myapp.local
```

If you forget, hostcraft will tell you:

```
✗ Error: Permission denied: run as Administrator to modify 'C:\Windows\System32\drivers\etc\hosts'
```

---

## Development

### Prerequisites

- [Rust]https://rustup.rs/ (2024 edition or later)

### Build from source

```sh
git clone https://github.com/Zaberahmed/hostcraft.git
cd hostcraft

# Build the entire workspace
cargo build

# Run directly without installing
cargo run --bin hostcraft -- list
cargo run --bin hostcraft -- add myapp.local 127.0.0.1
cargo run --bin hostcraft -- --file ./cli/hosts-copy list
```

### Reinstalling after changes

If you've installed via `cargo install` and made local changes, reinstall to pick them up:

```sh
cargo install --path cli --force
```

### Project structure

```
hostcraft/
├── core/                  # hostcraft-core — shared library
│   └── src/
│       ├── host/          # HostEntry, HostStatus, HostError + operations
│       │   ├── mod.rs     # Public API: parse_contents, add_entry, edit_entry, remove_entry, remove_entries_matching, toggle_entry, toggle_entries_matching
│       │   └── utils.rs   # Internal helpers
│       └── file/          # File I/O
│           ├── mod.rs     # Public API: read_file, write_file
│           └── utils.rs   # Internal: write_entries
│
└── cli/                   # hostcraft-cli — this crate
    └── src/
        ├── main.rs           # Entry point — parses args, wires background update check
        ├── command/
        │   └── mod.rs        # CLI definition (Cli, Command) + run() dispatch
        ├── display/
        │   ├── mod.rs        # Coloured output — all print functions
        │   └── style.rs      # ANSI style constants
        └── update/
            ├── mod.rs        # Update logic — public API and command handler
            └── utils.rs      # GitHub API, platform detection, binary download + self-replace
```

### Running tests

```sh
# Run all tests across the workspace
cargo test

# Run only core library tests
cargo test -p hostcraft-core
```

---

## License

MIT — see [LICENSE](LICENSE) for details.