Using Clojure in Command Line with Babashka

Image for post
Image for post
Ordinary Clojure code — can be run in REPL or in command line with Babashka.

Introduction

I never bothered to learn Bash so that I could be really fluent with it. If I needed anything beyond basic Bash stuff I immediately used Python in command-line scripting.

I’m currently implementing my Clojure simple server again, this time using the Integrant library. In this new version, I implemented three data stores: CSV, AWS DynamoDB, and Postgres. I had already implemented importing development data into DynamoDB (using Python), this time I used Babashka to import development data into Postgres — mainly just to have an excuse to try if I could replace Python with Clojure when scripting something with Bash.

The scripts can be found in my Clojure git repo in directory postgres.

Developing with Babashka

The really neat thing with Babashka is that you can develop your Babashka scripts as part of your Clojure project, or independently but using your favorite Clojure IDE. The picture above shows Clojure code in my favorite Clojure IDE, Cursive. I have the Clojure code that imports data into the Postgres database in directory postgres, so I have the following extra path in my deps.edn:

Then the nice thing is that I can develop the Clojure code as part of project’s other Clojure code. Let’s first create a short bash script that tells Babashka to run your Clojure code with a flag so that we know in the Clojure code when we are running the code using Babashka or using Clojure IDE REPL (file run-bb-load-data.sh):

The flag is the RUNNING_BB export above.

Then in the Clojure code we have a top level form (run-me) in the namespace (file bb_postgres.clj):

I.e. when reloading the namespace REPL runs the code but if the flag is not set it doesn’t actually do anything — it imports the code only if we are running the code using Babashka. The reason for this is that when I reload the namespace as part of my Clojure workflow I don’t want the data import to happen. For development purposes to test importing data, or any other function, I have a rich comment at the end of the file:

The comment block is here so that REPL does not run this code when reloading, of course. The function calls you see inside the comment block are just experiments added in no particular order. I can send any of these forms individually to be evaluated in the REPL - a typical Clojure trick when developing with REPL.

Babashka Use Cases

I really like the idea that I can now use Clojure in shell scripting. Of course I could use Clojure in shell scripting also without Babashka but JVM boot takes quite a long time which makes testing of the script in command line a bit painful. Not so with Babashka — Babashka boots lightning fast:

The use cases using Babashka in my personal scripting probably is a bit like I used Babashka to import data into the Postgres database in this exercise:

I.e. parsing CSV, transforming data, and then call some program with the transformed data, possibly read what was returned and do other stuff. You could possibly do all this using plain old Bash but I never bothered to learn Bash in that level that I can do more than test some flags and call other programs using Bash.

How Do I Use the Babashka Script in This Exercise?

I used Babashka to load development data into the Postgres data store. During development I built a custom Postgres image and provided a Just recipe to start the data store (file Justfile):

The run-docker-compose.sh starts the Postgres docker container, creates the schema and finally calls ./run-bb-load-data.sh which loads the data into the development Postgres data store:

Clojure (Babashka) vs Python in Shell Scripting

Let’s finally compare Python and Clojure (Babashka) when doing some Linux shell scripting.

Easiness. Both languages are pretty easy and fast to work if you have used them. Developing Python scripts is pretty fast — you just run the script in command line. Working with Clojure has one additional plus: you can use the Clojure REPL.

Library support: Python wins. When you are scripting in Python and you realize that it would be nice to use some AWS library — just use it (e.g. table_importer.py — the AWS boto3 library). The library support for Babashka is not as extensive, of course — but Babashka supports quite many namespaces outside clojure.core and also some additional libraries: Babashka built-in namespaces - keep eye on that page, maybe Babashka library support is growing in the future!

So, the library support might not be as good as with Python. But I really do love Clojure and if I’m implementing apps using Clojure it is really nice to do some ad hoc scripting using Babashka.

Conclusions

It’s nice to have another scripting tool in my toolbox: Babashka. Time will tell if I start using Clojure instead of Python as my preferred scripting language, thanks to Babashka. At least in this exercise Babashka did really well.

The writer is working at Metosin using Clojure in cloud projects. If you are interested to start a Clojure project in Finland or you are interested to get Clojure training in Finland you can contact me by sending email to my Metosin email address or contact me via LinkedIn.

Kari Marttila

The article was first published in https://www.karimarttila.fi/

Written by

I’m a Software architect and developer. Currently implementing systems on AWS / GCP / Azure / Docker / Kubernetes using Java, Python, Go and Clojure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store