My First Command Line Utility

I feel like a teenager who just discovered he has super-powers!

Angel showing off his wings
Image Source: Pop Mythology

Last year we gave our production servers the ability to send notifications to Slack and it’s been so helpful! Each one comes with all the relevant data, sometimes with a link to an admin page where you can fix the problem. It’s been truly empowering to deal with many types of problems without needing to write code.

One of the notifications is for a problem we can’t fix without input from our customers, so we needed to move the data from Slack to a Google Spreadsheet we could share with them.

Quickly discarding the idea creating an endpoint that gave me a CSV-formatted version of the data, I settled on copying the text straight from Slack (it was already there for the copy-pasting so don’t judge me) to a file and writing a program to process the Markdown-formatted data from Slack to generate the CSV.

The formatting program itself is nothing special, so straightforward I didn’t need to import a single library to make it work, but you wouldn’t believe how much more useful the program got after a tiny change at the beginning and the end of the code.

Inspired by the article I was reading (tried searching my history and honestly can’t find it — weird) and tired of changing the input and output file names in the code for each customer, I quickly changed the program to accept a stream of text as its input and output the result as a different text stream, transforming it into a command line utility. The difference was night and day.

With almost no effort I could stream a file into the util and redirect the output to wherever I want.

:: to command line output
node md_to_csv <
:: to a file
node md_to_csv < > notification.csv
:: to clipboard (copy + paste utility)
node md_to_csv < | clip

Working with text streams allowed me to interact with my OS’s native tools, turning a simple program into a command line utility. The ability to pipe its output gave me more options on how I could use it, and command line’s auto-complete reducing the amount of typing needed was icing on an already delicious cake.

Right now I feel so powerful I’m sure Dr Xavier’s already on his way here to invite me to his School for the Gifted.

Dr Xavier
Image Source: Hollywood Reporter