Fork me on GitHub
#babashka
<
2020-05-29
>
jeroenvandijk09:05:36

Nice! What’s your idea behind it? Command line webscraping?

borkdude09:05:30

That could work yeah. Or just general UI testing using babashka

borkdude10:05:47

Funny enough, the featured article on wikipedia, which is scraped in the example, mentions "pod" 😉 https://en.wikipedia.org/wiki/Main_Page

jeroenvandijk10:05:31

Yesterday I came across Cucumber again. I was thinking what if this could work with Graalvm. Getting https://github.com/babashka/pod-babashka-etaoin to work would be an important part here

borkdude10:05:28

the main difference is that etaoin uses clj-http and this uses clj-http-lite

borkdude10:05:00

you can now use the filewatcher pod with bootleg, to re-compile your static website. and then use the etaoin pod to refresh a browser, to preview it.

🤯 4
🆒 4
martinklepsch13:05:06

babashka + bootleg with it’s new pod support are kind of blowing my mind, awesome stuff 👏 🚀

❤️ 12
Crispin13:05:20

who knows what pods are yet to come? 😁

jeroenvandijk13:05:38

I fear singularity

jeroenvandijk13:05:46

We should stop it here

Crispin14:05:11

Probably wise words... It does feel a bit like drugs.

martinklepsch13:05:42

When running a script babashka establishes some namespace bindings, which is great. When turning a script into a namespace it is kind of tedious to recreate the same bindings. I’m wondering if there could be an easier way to do that like (default-bindings!) or similar (that approach would unfortunately trip up clj-kondo I think)

borkdude13:05:21

Do you mean the namespace aliases?

borkdude13:05:00

These are only there in the user namespace. Once you use your own ns form, you lose them.

borkdude13:05:35

I suggest not to use the user aliases in a script: https://github.com/borkdude/babashka#explicit-requires

martinklepsch13:05:40

I see, that makes sense I guess. Guess I’ll have to write a babashka script that adds requires for the user aliases to a script file 😛

martinklepsch13:05:50

Mostly kidding but I guess that wouldn’t be too hard to do 😛

Darin Douglass13:05:51

I think the last point in that link is the most pertinent: > Writing compatible code gives you the option to run the same script with clojure having bb be a lightweight replacement for lein run is wonderful

martinklepsch13:05:13

Is there a way to access *input* in a namespace’s -main?

borkdude13:05:48

This var only exists in the user namespace, and again, is mostly there for one-liners. If you really need it, and can't use something like (edn/read *in*) or something, then you can use user/*input*.

martinklepsch13:05:37

Would you like a PR adding a one-line note about this here: https://github.com/borkdude/babashka#input-and-output-flags ?

martinklepsch13:05:20

Would this mean that -I/`-i` and -m are generally incompatible?

martinklepsch13:05:33

If so then a warning in this case might be better than adding to the docs?

borkdude13:05:38

-m is not necessarily incompatible with the input flags

borkdude13:05:01

but the suggestion is to use it for one-liners since it can be kind of tedious to read from stdin in them

borkdude13:05:39

it's a trade off you should make if you want to write Clojure-compatible scripts

borkdude13:05:51

since *input* is not available in normal Clojure

borkdude13:05:36

it does work with -main

martinklepsch13:05:59

Is load-pod idempotent?

borkdude14:05:39

No. You can have multiple instances of one pod executable, by design.

borkdude14:05:58

There is also unload-pod which takes the identifier produced by load-pod.

borkdude14:05:08

so only use load-pod once if you only want to spin up one process

borkdude14:05:19

might be good to document

martinklepsch14:05:02

Interesting. Not that I’d need it but how would you dispatch stuff to a different pod?

borkdude14:05:29

With the lower level babashka.pods/invoke function

borkdude14:05:06

The story around having multiple pod instances of the same executable isn't completely clear cut yet, but I wanted to leave this option open

borkdude14:05:14

It might be useful for the filewatcher pod

martinklepsch14:05:07

yeah I can see how you wouldn’t want to commit to just one instance

martinklepsch14:05:33

Reusing the same pod across multiple namespaces is probably going to be a little difficult but stuff is fast so maybe it doesn’t even matter

martinklepsch14:05:01

yes, I’m following the repo & releases, saw the conversation about delayed requires etc. good stuff!

martinklepsch14:05:05

(require '[babashka.pods :as pods])
(pods/load-pod "bootleg")

(ns mkl.frontmatter
  (:require [clojure.string :as str]
            [ :as io]
            [clj-yaml.core :as yaml]
            [pod.retrogradeorbit.bootleg.glob :as glob]))
is this the right way to use pods inside namespaces?

borkdude14:05:52

if you do the load in the first required namespace, then the requires should just work in all the remaining ones

martinklepsch14:05:42

Right but I’m thinking that I might use the mains of these namespaces independently in which case that wouldn’t work

martinklepsch14:05:00

Made me wonder if maybe a --load-pod type CLI option would be a good idea

borkdude14:05:11

true. but you could make a stateful function which checks if the pod was already loaded, right?

borkdude14:05:34

(let [loaded? (volatile! false)]
  (defn load-pod []
    (when-not @loaded?
      (pods/load-pod ...)
      (vreset! loaded? true)))))))))

borkdude14:05:35

Eventually that might make sense, but I'd rather wait a bit with adding more CLI args, to give it a bit more thought

borkdude14:05:54

There is also BABASHKA_PRELOADS which you can use for this

martinklepsch14:05:03

makes sense… 🙂 I’m gonna play around with that approach a little bit

borkdude14:05:18

BABASHKA_PRELOADS="(pods/load ...)" bb script.clj

👌 4
martinklepsch14:05:56

Aside: I had a typo (def -main [] which ran correctly but caused an NPE just before it would otherwise exit with 0. Not sure if this is something that is to be expected but thought it was interesting that it still worked fine at first sight but then caused an error

martinklepsch14:05:15

BABASHKA_PRELOADS="(pods/load ...)"
that’s neat!

borkdude14:05:31

you can make an issue for the unexpected error

borkdude15:05:25

Yep, looks good

borkdude15:05:59

If you're going for a top level side effect, then I think even the volatile isn't needed, since the namespace is going to be loaded only once

borkdude15:05:46

But it surely can't hurt.

borkdude15:05:55

maybe defonce is even better here

martinklepsch17:05:34

I just deleted code from the snippet you gave me without thinking much hahaha

borkdude17:05:27

The cljc.java-time library is now compatible with babashka! https://github.com/henryw374/cljc.java-time/issues/10#issuecomment-636101690

😎 16
🎉 12
joshmiller19:05:09

Hopefully I haven’t missed something obvious: What’s the recommended way to use bb to set environment variables in the shell it’s running from?

borkdude19:05:24

Can you give an example of what you want to do?

joshmiller19:05:22

I am writing a script to assume an AWS role, which involves trading a password and an MFA code for a session token. Then that session token is set as an environment variable in the shell while you do work with it.

borkdude19:05:39

processes cannot set environment variables for parent processes

borkdude19:05:03

I'm not sure if that's what you are trying to do, but it sounds like it?

joshmiller19:05:15

Yes, unfortunately, although I think in my case I have a workaround with aws configure. Thank you for your help!

joshmiller19:05:01

It would be nice to be able to set variables like you can using source in bash, although I’m not close to expert enough to understand how that would be done.

borkdude19:05:02

You can use clojure.java.shell/sh and pass :env variables for subcommands

borkdude19:05:35

but you can't set environment variables for the shell which calls babashka. unless you spit out some file and source that later

joshmiller19:05:59

Right, the latter seems like the workaround if you need to set up a separate environment

borkdude19:05:38

$ source /dev/stdin <<< $(bb -e '(println "FOO=3")')
$ echo $FOO
3

joshmiller20:05:18

Oh awesome, thank you