Fork me on GitHub
#babashka
<
2023-08-30
>
pesterhazy07:08:34

I love the -x feature for building tools! It does, however, create a naming conundrum due to naming clashes 🧵

pesterhazy07:08:46

Suppose I'm writing a simple k/v store as a cli tool:

bb -x devtools.mymodule/get --key a

pesterhazy07:08:56

The code looks like this

(defn do-get [k]
   ...)

(defn get
  {:org.babashka/cli {:require [:key]}}
  [{k :key}]
  (println (do-get pit-dir k)))

pesterhazy07:08:01

Two questions: • devtools.mymodule/get clashes with clojure.core/get. But I'd rather not limit my external cli interface to names not occurring in clojure.core (so many good names are taken) • I typically write a pair of functions: do-foo - which is unit-tested - and also a CLI wrapper function foo which isn't unit-tested and just injects dependencies and supplies CLI args. But what is a good naming convention for this pair?

pesterhazy07:08:42

Maybe frivolous but it keeps bothering me

borkdude08:08:22

> But I'd rather not limit my external cli interface to names not occurring in clojure.core (so many good names are taken) Well, you can use (:refer-clojure :exclude [get]) but this gets old fast, since get is so commonly used, so perhaps (and also to address point two) you can just expose one cli namespace where you have these CLI wrappers and then you invoke your other functions from there?

pesterhazy08:08:07

yeah I could also create one .cli namespace per ns that needs it...

borkdude08:08:47

you can also do this via tasks in bb.edn: just give the task the name you want and then invoke the function using exec

2
borkdude08:08:14

e.g.:

{:tasks {get {:task (exec 'devtools.mymodule/get)}}}

pesterhazy09:08:55

That's a great option as well. The reason I really liked -x was that it doesn't require registering the task in a a centralized place (bb.edn). Instead the configuration is colocated with the code itself. But maybe I'll just need to drop one of these desiderata

DeepReef1113:08:08

If I want to create something that looks like vim with babashka (a cli that doesn't move line by line), does that need to be done through the shell?

borkdude13:08:21

You mean a TUI right?

borkdude13:08:47

You can use bblgum to do some basic stuff like displaying text, dialogues, etc: https://github.com/lispyclouds/bblgum This depends on gum to be installed. There's similar other stuff as well. It all works by shelling out indeed. An alternative is to build a TUI via #nbb and use something like ink. You can find that in the project README of nbb.

2
DeepReef1113:08:24

Oh great, thanks a lot!