How to build command line tools on shifting sands
The shifting tides of code and hype, deprecate and bit rot all the things. So, how do we build things that last? I recently built two command line tools and put careful thought into maximising their longevity. In this post I’ll share some of those thoughts.
Search hard for existing solutions
“Someone must have done something about this issue,” I thought while I searched frantically. But really, I was not able to find a command line tool that would convert a mysql connection string to connection parameters that could be fed to either the mysql or mysqldump commands.
# This is what I wanted to do, but could not. $ mysqldump mysql://username:email@example.com:3306/dbname
All the tools I found required you to manually decipher the connection string and break it up into separate arguments. Very easily done but it gets boring real fast.
Thus, I built mysql-parse which enables me to do this:
# This gets converted to # mysqldump -u username -ppw -H examplesite.com -P 3306 dbname $ mysqldump $(mysql-parse mysql://username:firstname.lastname@example.org:3306/dbname)
Don’t reimplement the world
mysql-parse is a tiny tool based on Node and quite useless without the mysql and mysqldump commands. I could have tried to mimic the mysqldump and mysql commands solely in Node but that would have been so much buggy work. Why try to compete with mature battle-hardened tools? Build upon them instead. Mysql and mysqldump aren’t going anywhere soon.
Have great test coverage
Put tests on all your APIs. And by APIs I mean the functions that programmers can find and use when including your package in their code. mysql-parse exposes two functions at the moment of writing, and both of them have tests. Arguably not great tests, but they’re there and they’re triggered on each code change thanks to Travis CI.
Fight feature creep
I originally built upon commander.js for fancy command line ergonomics (it’s a really great library), but after finishing mysql-parse I decided to remove it again. I realized that there was no use for a help command or a version command. The command just needs to check the first input parameter and shout if you don’t give it an input.
Building things that last
In closing I just want add that it’s peculiar how some code just keeps on ticking over the years while other code falls over at the first whiff of change.