Fork me on GitHub
#babashka
<
2024-06-26
>
richiardiandrea16:06:55

Hi folks, which "utility" library (like taonesso/encore) do you use that is fully compatible with babashka? Thanks for all the replies in advance 😄

borkdude16:06:35

This is a rather general question, since "utility" can mean all sorts of things

richiardiandrea16:06:52

Oh yes sorry it is rather generic and I meant if basically one of the libs like tupelo , encore, ...is known to be fully compatible with babashka. We use encore in our code base but that particular one is not compatible.

borkdude16:06:19

e.g. medley is a utility library which has some functions that should maybe belong in core. but do you want to use a utility library for the sake of using a utility library? gotta go now, dinner

👍 1
richiardiandrea16:06:07

yeah the answer is no - my next step would be to check what it contains and see if it matches our code base - I'll check medley thanks

borkdude17:06:49

I'd just write/maintain my own util lib in that case

borkdude17:06:54

which is custom to your need

ag19:06:19

I'm having déjà vu – I feel like I asked this question before, but I can't figure it out (maybe again). I can do something like this, right?

curl -s "" | bb --stream --nrepl-server
and then connect to it from my editor and once there:
(def data (json/parse-stream *in* true))
Then I can analyze the data however I want. Now, how do I do the same thing but in the same terminal session? I can even do this:
curl -s "" | bb -i -e '(json/parse-stream *in* true)'
but that just prints the results, and I want it to remain in the repl

borkdude21:06:43

probably it's best to just start the console REPL, then use bb.http-client or so to fetch the json, parse the json and then handle it as you like, without using any bash magic or bb i/o flags

borkdude21:06:08

unless @U013JFLRFS8 has a better suggestion, I saw he was just typing something :)

Bob B21:06:43

I was just gonna say that I think -e and the repl are sort of (currently) mutually exclusive, and was going to try and throw together some bash magic

😆 1
Bob B21:06:50

Actually, now that I think about it, since the piping would occupy stdin, the repl wouldn't be able to read input from the terminal, so the curl would probably need to become a pseudo-file

👍 1
borkdude21:06:03

bb does support --init btw, where you can supply a file which is ran before anything else is done. so bb --init init.clj repl would work

ag21:06:49

what about preloads?

borkdude21:06:21

that also works

borkdude21:06:42

$ BABASHKA_PRELOADS='(prn :dude)' bb repl
:dude
Babashka v1.3.185 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=>

borkdude21:06:16

(I am on an old laptop here, laying on the couch, hence the old bb version)

ag21:06:54

I mean, personally, for me, the first case with the nrepl-server works the best. I was just trying to show someone how easy it is to use Babashka to sift through API response data with very simple one-liner. I thought it would be simple, I guess, not through piping. And then it turned into a stupid question to quench my curiosity.

Bob B22:06:52

I cheated a bit by using slurp, but:

$ bb --init <(echo '(def x (json/parse-string (slurp "") true))')
Babashka v1.3.191 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (keys (first x))
(:breed_group :reference_image_id :name :life_span :bred_for :weight :id :temperament :origin :height)

metal 1
ag22:06:10

My attempt: BABASHKA_PRELOADS="(do (require '[babashka.http-client :as http]) (def d (-> \"\" http/get :body (json/parse-string true))))" bb repl

ag22:06:41

Holy cow, I had no idea you could slurp urls... TIL

borkdude22:06:04

it works in a limited number of cases

borkdude22:06:15

e.g. it doesn't work very well if the url needs redirection

ag22:06:10

yeah, but... I had no idea you could do this even for simple gets... I'm like totally befuddled. Clojuredocs even have the example right there. It just never occurred to me to read the docs for slurp. It always worked and I never needed to check the docs. I don't like PLs for which you have to open the docs, even for the simple stuff like reading a file.

ag22:06:22

Thank you friends.

❤️ 1
Bob B22:06:26

and if you want to get crazy, a little tweak to the portal CLI script that bbin can install could be used to pop a portal window with some json that comes from curl'ing, but I'm maybe going a bit too far here

👍 2
😯 1
ag22:06:25

Whoa. It never seizes to amaze me. You ask Clojurians a question for a simple one-liner, and they'll take you on a wild, mind-blowing journey. To find the answers for the ultimate questions of life, the universe, and everything... 🙂

ag22:06:41

Holy mother of Alan Turing, that even worked:

bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.56.0"}}}' --init <(echo '(require (quote [portal.api :as p])) (def p (p/open)) (def data (-> "" slurp (json/parse-string true))')
Now, can you tell me guys, how to tap to inspect data? I've never used Portal before.

Bob B22:06:19

from <https://github.com/rads/portal-cli/blob/main/src/rads/portal_cli.clj>:

(p/open)
(p/tap)
(tap> data)
you did open already, so it should just need the other two - that command seems to be missing a paren when I ran it, but after fixing that, p/tap and tap> did the trick

ag22:06:33

Whoaah... dogs, cats and avocados

bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.56.0"}}}' --init \
    <(echo '(require (quote [portal.api :as p])) \
    (def p (p/open)) \
    (def data (-> "" slurp (json/parse-string true))) \
    (p/tap) \
    (tap> data) \
    ')
This is awesome.

👍 2