Ramblings of an aging IT geek
← Ramblings of an aging IT geek
rust

i rewrote a thirty-line shell script in rust

A small log-tailing CLI I'd been keeping as a bash script, rewritten in Rust with clap, and an honest accounting of whether the effort paid off.

A code editor showing a Rust source file

I had a shell script. It tailed a few log files, filtered for the request ID I cared about, and coloured the output by severity. Thirty-odd lines of bash held together by awk, tail -F and a wish. It worked, mostly, until a log rotated and tail got confused, or until I wanted it on a machine without GNU coreutils and the sed flags didn't match.

So I rewrote it in Rust. The honest question is whether that was sensible or whether I just wanted an excuse.

The rewrite was pleasant. clap with the derive feature gives you argument parsing that documents itself, and the --help output is better than anything I'd have written by hand for the script. notify handled the file-rotation problem properly: I watch the directory, not the inode, so when the log rotates I reopen cleanly instead of staring at a stale handle. anyhow for errors at the top, thiserror for the one library-ish module. None of this is novel, it's just the boring well-trodden path, which is exactly what you want for a tool you'll forget the internals of.

A close-up of a terminal running a program

The build took about forty minutes of my evening, most of it spent deciding how to colour output (owo-colors, in the end, because it respects NO_COLOR without me doing anything). A single static binary drops onto any of my machines with no runtime to install. That alone justifies it for me, given how often the bash version broke on a box with the wrong userland.

Was it worth it? For a script I run several times a day, that now never silently mishandles a rotated file, yes. The binary is 2.3 MB, starts instantly, and I've not touched it since. If this were a one-off I'd run twice and bin, absolutely not, the bash version would have been the right answer and the Rust version pure indulgence.

The thing I keep noticing is that Rust has quietly become a reasonable choice for small CLIs, not just systems work. The ecosystem for the unglamorous parts (arg parsing, file watching, terminal colour, error context) is mature enough that you spend your time on the actual logic. That wasn't true a few years ago. It is now, and that shift is the real story, not my little log tailer.