babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
grazfather 2021-04-06T00:11:07.397100Z

is bb doc not supported? tasks are looking good. depends are looking good, and bb doc should show depends!

borkdude 2021-04-06T06:58:31.397700Z

Not yet for tasks, I will fix that soon

1👍
pithyless 2021-04-06T11:55:42.402500Z

I've played with 0.3.1 before the weekend and came up with this kind of pattern:

bb -m my.runner/run some.ns/fn :a b :c 123 :d '#{:e :f "g" [:h]}'
So it works just like the tasks demo, but it will parse all args as EDN and will coerce unqualified symbols to strings (so in this case b would be a string argument). The nice thing about this is that all my tasks now just take a map of options, so I can reuse all the destructuring syntax without having to manually parse CLI arguments and the functions compose nicely. I'm kind of assuming I can pass in a flat map of keyword arguments and each task can just consider the ones it's actually interested in.

pithyless 2021-04-06T11:57:19.404400Z

Since I'm wrapping the babashka main functionality, I could add some conveniences: if you pass an unqualified symbol as the first argument, it assumes you're calling a function in the tasks ns; and I also have a my.runner/doc that is similar to bb doc and a my.runner/echo which is kind of a --dry-run debug

pithyless 2021-04-06T12:00:13.407Z

I'm wondering if the parsing of CLI arguments as a map of keyword/edn-values may be popular enough to be supported more directly by a babashka helper (ala process/tokenize ). Either way, just giving a positive experience report -- and bb.edn is very helpful! 🙂

1👍
borkdude 2021-04-06T12:01:22.408Z

@pithyless Interesting! the clojure CLI also supports passing map args as EDN, was that your inspiration?

pithyless 2021-04-06T12:01:32.408200Z

yes

pithyless 2021-04-06T12:02:07.408900Z

actually, I was hoping there was some code I could call directly, but my brief search was not fruitful

pithyless 2021-04-06T12:03:17.409500Z

so I ended up heavily borrowing the ideas from this post: https://vlaaad.github.io/tools-cli-in-10-lines-of-code

pithyless 2021-04-06T12:07:23.414300Z

I actually considered the idea of "default args", "command args", and "env args" could all be considered the same thing, if we try to map the CLI to the Clojure idea of flat maps with fully-qualified keywords. So maybe instead of: SOME_ENV=... command --verbose --some=args we could just say runner some.ns/command :some.env/stuff :value1 :some.cmd/arg :value2 :verbose true

pithyless 2021-04-06T12:08:02.415300Z

and I could imagine that we could just merge mutiple maps (maybe part of it is just pulled from some file/env/wherever)?

richiardiandrea 2021-04-06T17:55:59.416500Z

Hi there I have two babashka scripts that parse lines of edn from stdin, is there any practical difference between using a straight -f vs using a main with -m?

richiardiandrea 2021-04-06T17:58:22.417Z

(it's a migration script)

richiardiandrea 2021-04-06T18:00:33.417200Z

it seems from the manual that running a uberjar would not allow me to call two different namespaces/files?

borkdude 2021-04-06T19:47:45.418600Z

@richiardiandrea I don't recommend using the -i -O flags with scripts really, they were mostly designed for one-liners. Just use edn/read if you need to read multiple lines of edn for example.

borkdude 2021-04-06T19:49:13.419Z

I documented here: https://book.babashka.org/#_scripts what the scripting alternatives are for the i/o flags

1👏
borkdude 2021-04-06T19:50:25.420100Z

There is not really a practical difference between -f and -m. The main difference is that with -m the function you are invoking is explicit and with -f it's implicit via side effects

richiardiandrea 2021-04-06T19:52:49.420400Z

Just wanted to process things lazily from the shell piping as they come

borkdude 2021-04-06T19:53:09.420800Z

When using an uberjar the main function must be set when creating the uberjar

borkdude 2021-04-06T19:53:24.421100Z

or you can invoke it like this: bb -cp foo.jar -m foo

borkdude 2021-04-06T19:53:46.421200Z

You can still do that, using *in*

borkdude 2021-04-06T19:57:05.421400Z

@richiardiandrea

$ echo '1 2 3' | bb -e '(take-while #(not= % ::EOF) (repeatedly #(edn/read {:eof ::EOF} *in*)))'
(1 2 3)

borkdude 2021-04-06T19:57:17.421600Z

that is a lazy seq of edn vals from stdin

richiardiandrea 2021-04-06T19:59:01.421800Z

Yes that's what I am doing and I have a def at the top level for that. So do one-liners need them?

richiardiandrea 2021-04-06T20:00:06.422Z

@borkdude sorry I also had this side question 😃 I have.inspected the code and depstar only triggers one main. I basically need to call two mains 😃 in two different namespaces

borkdude 2021-04-06T20:01:01.422200Z

I don't understand your question. If you use the above construction, you don't need any special flags

borkdude 2021-04-06T20:01:49.422400Z

I think you would need to create one main function that combines the two

richiardiandrea 2021-04-06T20:08:55.422600Z

yeah that is not possible cause the two are piped one to the other in the script...can I do something like bb my.jar -m my-foo.main and then bb my.jar -m my-bar.main at the moment?

borkdude 2021-04-06T20:13:29.422800Z

you can do bb -cp my.jar -m my-foo.main

borkdude 2021-04-06T20:15:10.423Z

so bb -cp my.jar -m foo/main1 | bb -cp my.jar -m foo/main1

borkdude 2021-04-06T20:16:09.423300Z

but why use a pipe between two functions if you can just process the data within one process?

richiardiandrea 2021-04-06T20:17:25.423500Z

because I want to expose the intermediate result and possibly re-run it

richiardiandrea 2021-04-06T20:17:41.423700Z

it's some form of aid for my operators

richiardiandrea 2021-04-06T20:18:09.423900Z

the intermediate result can be inspected for problems, solve them, then run against it multiple times

richiardiandrea 2021-04-06T20:18:36.424100Z

thank you I will try your solution, it looks like it might work

richiardiandrea 2021-04-06T20:19:29.424300Z

never mind, I wanted to just understand when I need -IO if at any point I can use *in*

richiardiandrea 2021-04-06T20:19:59.424500Z

what do these flags do under the hood basically, but I am checking the source right now no worries

borkdude 2021-04-06T20:32:11.424700Z

the flags i and I influence the value of *input* and -o or -O influence how results get printed

richiardiandrea 2021-04-06T20:41:46.425Z

oh I see, so -o/`-O` is basically only useful if I don't println/`prn` myself and the script ends evaluating to a data structure...gotcha that makes sense

borkdude 2021-04-06T21:11:23.425200Z

correct

1❤️
richiardiandrea 2021-04-06T21:18:30.425500Z

well no it does not seem to work... This is what I see with jar -tf my-jar

bb.edn
src/
src/image_migration/
src/image_migration/insert_images.bb
src/image_migration/csv_to_image_todos.bb
But then when I run bb -cp "$uberjar_path" --verbose -m image-migration.csv-to-image-todos/-main It throws a:
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Could not find namespace: image-migration.csv-to-image-todos.
Location: <expr>:1:10
I am trying to understand now if I am donig something obviously stupid 😄

borkdude 2021-04-06T21:19:52.425700Z

does $uberjar_path have the correct value?

borkdude 2021-04-06T21:22:00.425900Z

@richiardiandrea I see, the expected directory structure here is image_migration/insert-images.bb, not with src prefixed

borkdude 2021-04-06T21:22:13.426100Z

so I think something went wrong in your uberjar production

richiardiandrea 2021-04-06T21:23:13.426300Z

uhm ok let me see there

richiardiandrea 2021-04-06T21:23:54.426500Z

bb.edn is

{:paths ["src" "resources"]}
and I call bb uberjar $(UBERJAR) in a Makefile

borkdude 2021-04-06T21:24:14.426700Z

ah I see... there is a bug here. the uberjar task does not yet take into account bb.edn :/

borkdude 2021-04-06T21:24:18.426900Z

I logged an issue for this

richiardiandrea 2021-04-06T21:24:32.427100Z

ok glad we found it

borkdude 2021-04-06T21:24:53.427300Z

so for now you will have to do it like this:

bb -cp src:resources uberjar $(UBERJAR)

1💯
richiardiandrea 2021-04-06T21:27:55.427600Z

FYI the command with no -cp before was including all the files in the folder where you were executing it

borkdude 2021-04-06T21:28:10.427800Z

yeah, that's also not good.

richiardiandrea 2021-04-06T21:28:13.428Z

this one now just includes the correct stuff in -cp

richiardiandrea 2021-04-06T21:28:17.428200Z

yeah

richiardiandrea 2021-04-06T21:29:02.428400Z

but this one works! bb -cp "src:resources" --uberjar $(UBERJAR) 🎉

borkdude 2021-04-06T21:29:12.428600Z

cool! I logged the issue here: https://github.com/babashka/babashka/issues/776

1👍
richiardiandrea 2021-04-06T23:44:40.429100Z

PR ready for the former issue https://github.com/babashka/babashka/pull/777