Simple Zig CLI tool build
Rich Harris created a tool a long time ago called degit
. It lets you use git repositories as project templates. You just point degit
at a repo, then it clones it and re-initializes it as a new repo. Pretty simple.
I keep coming back to this tool over heavier-handed scaffolding tools. It’s unfortunate that I can’t use it at all! My problem is that degit
only works with a specific set of git hosting sites. AFAIK it will only pull from Github, Gitlab, and Bitbucket. Maybe others, but definitely not the private repos we use at my company.
Note: It turns out there is at least one fork of
degit
that fixes this issue. It’s calledtiged
.
Today I decided to just build a similar tool that works for me. I figured I could whip one up in Powershell pretty quickly. I’m not a Powershell wizard, but Claude is.
After that, I figured I would try to rewrite it in Zig. I have been looking for a small, but non-trivial Zig project. And this actually made a lot of sense.
The tool itself mostly spawns some child processes and shuffles some files around. So I was able to get familiar with Zig again, learn something about the std.fs
and std.process
, and didn’t have to worry much about manual memory management.
The original design did the following:
- Clone the template repo to a temporary folder.
- Remove the existing
.git
folder. - Copy the files to the destination folder.
- Reinitialize the repo in the destination folder.
- Delete the temporary folder.
I got this working except for the last item. I would get runtime errors when attempting to delete the temp folder. I could not figure out the obtuse errors, so I just decided to simplify the project to it’s barest requirements.
- Clone the template repo to the destination folder.
- Remove the existing
.git
folder. - Reinitialize the repo in the destination folder.
Claude kind of sucks at writing Zig code at the moment. Mostly, it kept guessing wrong about functions on std.fs
, which is probably because the standard library is in constant flux. Or maybe it was guessing based on other languages. But, as usual, if you can give Claude some clues it can at least help you go in the right direction.
It also had great explanations for important concepts, like which allocators make the most sense for this use case, and why. In fact, now I think the problem with the temp folder might have come down to the allocator I was using. I’ll have to experiment and see!
If you’re interested, the tool is at https://github.com/tofraley/scaf.