babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
2021-05-27T06:09:04.210100Z

I'm looking for a task scheduler / cron-like-thing that's happy in babashka, any suggestions?

borkdude 2021-05-27T07:28:27.210700Z

What's the use case? How would you do this in normal Clojure?

borkdude 2021-05-27T06:43:08.210500Z

Probably just use cron?

Steven Katz 2021-05-27T14:57:36.212Z

Hi, I would like to confirm my understanding of how to run the aws cli from a babashka file. I would use “https://book.babashka.org/#_clojure_java_shell” as described in the book? Is this the prefered way?

borkdude 2021-05-27T14:58:39.212900Z

@steven.katz it depends. if you would like to capture output, or only like to cause a side effect and don't want to use the output, but see the output as you execute it

Steven Katz 2021-05-27T14:59:36.213700Z

hmm, not sure, right now I guess I don’t really need to capture the output, just look at it as it executes

borkdude 2021-05-27T15:00:51.215200Z

In that case babashka.process offers a better alternative:

(require '[babashka.process :refer [process]])
@(process ["aws-cli" "your-argument1" "your-argument2"] {:inherit true})

borkdude 2021-05-27T15:02:04.216600Z

The shell function from babashka tasks can also be used, it basically the same with a slightly different API, but with defaults for running it as a task:

(require '[babashka.tasks :refer [shell]])
(shell "aws-cli your-arg1 your-arg2")

Steven Katz 2021-05-27T15:02:25.216900Z

Great, thanks!

skuro 2021-05-27T15:27:48.217900Z

I'm trying to use babashka in my CI environment, currently trying to just install the binary and make it work on a "vanilla" alpine image. here's my trouble:

bash-5.0# wget -O /tmp/bb-install <https://raw.githubusercontent.com/babashka/babashka/master/install>
Connecting to <http://raw.githubusercontent.com|raw.githubusercontent.com> (185.199.111.133:443)
bb-install           100% |***********************************************|  3504  0:00:00 ETA
bash-5.0# bash /tmp/bb-install
Downloading <https://github.com/babashka/babashka/releases/download/v0.4.3/babashka-0.4.3-linux-amd64.tar.gz> to /tmp
Successfully installed bb in /usr/local/bin
bash-5.0# which bb
/usr/local/bin/bb
bash-5.0# bb
bash: /usr/local/bin/bb: No such file or directory

skuro 2021-05-27T15:27:55.218100Z

not sure what's going wrong tbh

borkdude 2021-05-27T15:28:28.218500Z

@skuro for Alpine you need the --static argument of the installer script

skuro 2021-05-27T15:29:44.219Z

oh I see

borkdude 2021-05-27T15:30:36.219400Z

perhaps we can switch to this as the default on linux at one point, @rahul080327 @thiagokokada

skuro 2021-05-27T15:31:32.219700Z

mhhh

skuro 2021-05-27T15:31:45.220Z

bash-5.0# /tmp/bb-install --static
Downloading <https://github.com/babashka/babashka/releases/download/v0.4.3/babashka-0.4.3-linux-amd64-static.tar.gz> to /tmp
mkdir: unrecognized option: static

borkdude 2021-05-27T15:32:24.220300Z

hmm, a bug in the script?

skuro 2021-05-27T15:33:33.220600Z

install_dir seems to become --static

borkdude 2021-05-27T15:34:14.221Z

ah here's the bug:

if [[ $# -eq 1 ]]; then
   install_dir=${1:-}
fi

skuro 2021-05-27T15:34:18.221200Z

yep

borkdude 2021-05-27T15:34:38.221500Z

we can remove that part I guess, just use explicit --dir ...

borkdude 2021-05-27T15:34:48.221700Z

lemme do that

borkdude 2021-05-27T15:36:19.222200Z

fixed, but github caches it for a while, so try again in some minutes

lispyclouds 2021-05-27T15:48:46.222300Z

At this point I'm much more confident in making it static everywhere should be safe

lispyclouds 2021-05-27T15:48:57.222500Z

Maybe we can do an A/B test somehow 😛

borkdude 2021-05-27T15:52:29.223Z

@skuro the issue should be fixed now I think

skuro 2021-05-27T16:00:20.223200Z

Thanks I'll try again later

borkdude 2021-05-27T16:01:44.223600Z

@skuro there is also a bb alpine image btw:

babashka/babashka:0.4.3-alpine

borkdude 2021-05-27T16:21:37.223700Z

hehe rand-int

skuro 2021-05-27T17:08:53.223900Z

Oh ok, missed that completely, i blindly tried borkdude/babashka:alpine

kokada 2021-05-27T17:12:55.224Z

Maybe, but something to keep in mind is that maybe musl may have some difficults doing requests in some configurations (thanks to the fact that it embeds its own DNS resolver instead of using NSS)

kokada 2021-05-27T17:15:48.224300Z

The cases where (and if) this will cause issues should be rare, so IMO it is safe to switch, but I think it is important for people to understand the implications of static binaries

borkdude 2021-05-27T17:31:49.224500Z

ok, we'll just leave it for now

borkdude 2021-05-27T17:54:56.229300Z

I deleted borkdude/babashka and borkdude/clj-kondo on Dockerhub now. Let's see how many questions I will get and how many CIs will break... tl;dr: use babashka/babashka and cljkondo/clj-kondo

kokada 2021-05-27T17:55:43.230300Z

Let me check nixpkgs. I think I migrated both, but not sure

BuddhiLW 2021-05-27T17:56:51.231300Z

Hello! I would like to load a babashka file, which only contains "helper-functions" for what I would like to script. In the end, I'm loading this file on a closh shell, and running a pipe. 1. First problem I have, when I run bb -f partitioning.clj It returns, as defined, only the last-defined function in the file, #'devibrary.partitioning/create-populate-dir The integral code can be found in, https://github.com/BuddhiLW/CloshBashika/blob/main/devibrary/src/devibrary/partitioning.clj Q: How could I load all the functions properly?

borkdude 2021-05-27T17:58:12.231500Z

What you see is just the printed result.

borkdude 2021-05-27T17:58:25.231700Z

Babashka prints the last return value

borkdude 2021-05-27T17:59:02.231900Z

so the file is loaded.

borkdude 2021-05-27T18:00:31.232100Z

Maybe I should have been more careful? On the other hand, it's been 6 months now

kokada 2021-05-27T18:01:12.232300Z

Well, at least for nixpkgs seems ok

kokada 2021-05-27T18:03:43.232500Z

> Maybe I should have been more careful? On the other hand, it's been 6 months now If you really want people to fix their CIs breaking it is the only way 🤷 Those group renamings on GitHub are really bad on UX side (since it only shows when pulling/pushing)

BuddhiLW 2021-05-27T18:09:17.232800Z

Seems like I'm unable to use the functions inside the namespace

BuddhiLW 2021-05-27T18:14:41.233500Z

Also, if I manually load the function on the closh environment, it works fine.

BuddhiLW 2021-05-27T18:16:15.233900Z

I thank you for your time, @borkdude. I'm not really in a rush. If anyone knows what is happening would be great. But, for now, loading things in the shell has been working fine, for my scope/work.

kokada 2021-05-27T18:25:28.234100Z

Ah, just saw that you're referring to the Dockerhub (I missed it and thought it was the GitHub repo 🤦 )

borkdude 2021-05-27T18:25:44.234300Z

no

2021-05-27T18:26:48.234500Z

@pedrogbranquinho I’m guessing the problem is that you are not calling a “main” function. Like @borkdude said “Babashka prints the last return value”. So the last value is a function

2021-05-27T18:27:31.234700Z

If you put (your-function) and the end of the file it should run

2021-05-27T18:30:01.234900Z

A common pattern is

#?(:bb (apply your-main-function *command-line-args*))
This will run only in babashka. If you want to only run when it is invoked from the command line (and not when requiring it from another ns) you can add an additional check

2021-05-27T18:31:00.235100Z

Maybe this is also an option https://book.babashka.org/#main-function

2021-05-27T18:33:52.235500Z

@pedrogbranquinho I’m rereading the thread and I’m not sure if I actually understand your problem. Is it your goal to invoke this function from the command line? https://github.com/BuddhiLW/CloshBashika/blob/main/devibrary/src/devibrary/partitioning.clj#L68

kokada 2021-05-27T19:44:49.237Z

Is there some way in Babashka that I can set the directory where a script/project is running, without needing to change the directory :thinking_face: ? Context in 🧵 .

kokada 2021-05-27T19:45:57.237100Z

I have a Babashka project (has a bb.edn and src, test directories) that needs to be accessed from anywhere (so I added a bin directory from this project to my PATH)

lispyclouds 2021-05-27T19:46:34.237300Z

Maybe this helps: https://github.com/bob-cd/bob/blob/main/bb.edn#L30 the way im using tasks in this mono-repo?

kokada 2021-05-27T19:47:10.237600Z

Nowadays what I do to load the bb.edn (that includes this project deps, classpath, etc) is to have a hybrid shell script/babashaka script that cd into the script directory and runs the program

kokada 2021-05-27T19:47:32.237800Z

@rahul080327 I don't think this will work because of the next reason

kokada 2021-05-27T19:49:08.238100Z

The approach above worked fine, but today I discovered that I can't change to the project directory for one reason: I will lose the directory where the user is running the project

kokada 2021-05-27T19:49:46.238300Z

So I have another command that tries to download some things on the current directory, but now it is always downloading it to the directory that my project is hosted (since I changed the directory before running the project)

kokada 2021-05-27T19:50:17.238500Z

I can't change this command because it is not called directly by my project (it is called indirectly in a fallback flow)

kokada 2021-05-27T19:52:08.238700Z

The major issue that I am facing now is that while bb -f /path/to/script.clj works, it "ignores" the bb.edn file

kokada 2021-05-27T19:52:56.239Z

So if we could have something like bb -f /path/to/script.clj --bb-file "/path/to/bb.edn, I think this would solve the problem

kokada 2021-05-27T19:53:59.240Z

(BTW, this works in Python, since a venv can be called anywhere if you, just by calling your script with path/to/venv/bin/python)

2021-05-27T19:54:14.240500Z

sorry if this is a common qustion, i'm new to this is there an existing time expiring cache that is happy in babashka?

(def devices (cache/ttl-cache-factory #{} :ttl 5000))
this seems to be missing the sorted protocol

BuddhiLW 2021-05-27T19:54:21.240600Z

Yes, actually, I wish to use it as a command in a pipe. In the repository I prepared a demonstration. Let's say you have shifted-ordered files. So, text5.txt, text6.txt, ..., text10.txt. You can rearrange them removing this shif-value of 5. And, you can change their regex "text", as well as the extension ".txt". All this with this one-liner which is a defcmd-macro (from closh). Then, let's say you want to partition these files in sub-folders with determined regex base-name. You can then use another simple pipe, twoliners. (to reproduce the results, you can go to ./sh-scripting/, run sh lupo-setup.sh , then, cd ../test/ , and run the following:

$rename-test "test" ".txt" 5 "demo" ".org"

$ls |&gt; (map #(file-ids % "demo" ".org")) |&gt; #(populate-key-map (create-range [0 3 7 10]) % 1) |&gt; #(create-populate-dir % "dir-demo" "demo" ".org")
This rename files from text5.txt, ... , text15.txt to http://demo0.org, ... , http://demo10.org. Then, move these demo[0,3].org to dir-demo1; [4,7] to dir-demo2; and, [8,10] to dir-demo3.

kokada 2021-05-27T19:55:59.241Z

> So if we could have something like bb -f /path/to/script.clj --bb-file "/path/to/bb.edn, I think this would solve the problem Actually, this would need to be a --context /path/to/project, since the :path (and probably other things) on the bb.edn file are relative

lispyclouds 2021-05-27T19:57:56.241300Z

maybe launch bb with the --classpath set to the place with the src etc and launch it from the place bb.edn is? Im not sure if the bb.edn path could be specified :thinking_face:

kokada 2021-05-27T19:58:45.241500Z

I think this could work, however I would need to load my dependencies manually somehow

lispyclouds 2021-05-27T19:58:54.241700Z

i think the bb.edn is also picked up from the --classpath afaik

kokada 2021-05-27T19:59:04.241900Z

Huh...

kokada 2021-05-27T19:59:06.242100Z

Let me try

lispyclouds 2021-05-27T19:59:40.242300Z

could be wrong 😅

kokada 2021-05-27T20:00:21.242500Z

Well, I can set my --classpath to path/to/project/src, but this will not load the dependencies

kokada 2021-05-27T20:00:50.242700Z

Using --classpath path/to/project will not find my project namespaces

kokada 2021-05-27T20:01:50.242900Z

Passing both (`--classpath path/to/project:path/to/project/src`) also doesn't work

lispyclouds 2021-05-27T20:02:03.243100Z

how are you invoking bb? providing a particular ns with the classpath?

BuddhiLW 2021-05-27T20:02:15.243300Z

As it is, I need to load all the functions in closh repl, to use these one-liners - which defeats the purpose a bit. I could make some tweaks and write it in "pure babashka" syntax, and leave closh aside. Currently, I'm maintaining both versions in parallel. I guess I could, then, write the commands to execute inside the script - as I noticed that was the purpose, as you spoke. But, going further in this problematic, do you see an easy way I could load these namespaces and use in a pipe | bb -e (ns-function) fashion?

kokada 2021-05-27T20:02:49.243500Z

I actually call the script itself, that loads a namespace from the project, that them loads the -main function 😅

kokada 2021-05-27T20:03:15.243700Z

Because I need to do some setup (and I try to avoid as much bash as possible)

kokada 2021-05-27T20:03:37.243900Z

But well, I think parsing the :deps from bb.edn should work

2021-05-27T20:04:10.244100Z

It’s hard to understand the exact problem you are trying to solve. Can you try to isolate the issue? Is this specific to closh or is this also happening in the bash shell?

lispyclouds 2021-05-27T20:04:59.244400Z

yeah i could imagine shoving the entry point into bb.edn and calling the code from that instead of the scripts?

2021-05-27T20:05:23.244600Z

For piping process it’s good to have a look here https://book.babashka.org/#_input_and_output_flags

lispyclouds 2021-05-27T20:05:37.244900Z

invoke bb with a classpath and a task name maybe

kokada 2021-05-27T20:07:55.245200Z

I don't think this would work anyway in my case since to read bb.edn, AFAIK you need to be in the same directory as the bb.edn is created

lispyclouds 2021-05-27T20:09:09.245400Z

right, im not sure of this, @borkdude should help better 🙏:skin-tone-3:

BuddhiLW 2021-05-27T20:10:53.245600Z

I eval a babashka file. I want to call, now, the functions which were loaded in this eval. I can't. That's basically the isolated issue there.

BuddhiLW 2021-05-27T20:13:10.246Z

(irrespective of what shell I'm in)

2021-05-27T20:17:04.246200Z

ok so first thing is that bb loses it’s state between 2 calls. So the first one is unrelated to the second one

2021-05-27T20:17:24.246400Z

Let me see if I can give you a working version

BuddhiLW 2021-05-27T20:19:01.246600Z

I appreciate it @jeroenvandijk

2021-05-27T20:29:23.246800Z

You are missing a bb.edn file in the devibrary dir

2021-05-27T20:29:33.247Z

Here is some background https://book.babashka.org/#project-setup

kokada 2021-05-27T20:29:50.247300Z

This works 😄

#_(
   export PROJECT_DIR="$(cd "$(dirname "$(dirname "$0")")"; pwd -P)"
  
   exec bb -f "$0" -- "$@"
  )

;; Add debugging helpers and dependencies
(require '[babashka.fs :as fs]
         '[babashka.deps :as deps]
         '[babashka.classpath :as cp]
         '[clojure.edn :as edn])

(defn- file-from-project-dir [filename]
  (str (fs/path (System/getenv "PROJECT_DIR") filename)))

(deps/add-deps (edn/read-string (slurp (file-from-project-dir "bb.edn"))))
(cp/add-classpath (file-from-project-dir "src"))

2021-05-27T20:29:51.247500Z

bb -m devibrary.core/foo 2
2 Hello, World!

2021-05-27T20:30:17.247700Z

That should work when you add a bb.edn file like this one

{:paths ["src"]}

kokada 2021-05-27T20:30:38.247900Z

Kinda hacky? Maybe. But not bad (better than bash)

2021-05-27T20:30:55.248200Z

The other function can be called like bb -m devibrary.partitioning/create-populate-dir 1 2 3 4

2021-05-27T20:31:02.248400Z

I have it working here

2021-05-27T20:32:13.248600Z

➜  CloshBashika git:(main) ✗ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add &lt;file&gt;..." to include in what will be committed)
	devibrary/bb.edn

nothing added to commit but untracked files present (use "git add" to track)

2021-05-27T20:32:25.248800Z

@pedrogbranquinho Hope this is what you needed

BuddhiLW 2021-05-27T20:37:35.249100Z

Yes, it is! I will dive more deeply in the documentation, also. Because, certainty I will be using it in daily use. Thank you @jeroenvandijk and @borkdude

2021-05-27T20:38:37.249300Z

np @pedrogbranquinho. Hope you are having fun 🙂 Also cool to see you are using Closh

🎉 1
lispyclouds 2021-05-27T20:41:15.249500Z

yeah i guess having the ability to specify the bb.edn path should be the best option

borkdude 2021-05-27T20:41:15.249700Z

@thiagokokada I don't know if this solves your problem, since I didn't read the entire thread, but: > bb -f /path/to/script.clj --bb-file "/path/to/bb.edn you can do this:

BABASHKA_EDN=/path/to/bb.edn bb -f path/to/script.clj

lispyclouds 2021-05-27T20:41:27.249900Z

ahh

borkdude 2021-05-27T20:41:34.250100Z

but note that BABASHKA_EDN is not documented and only used for testing right now

lispyclouds 2021-05-27T20:42:08.250300Z

we were missing the --bb-edn flag and missed it in the help too

borkdude 2021-05-27T20:42:40.250700Z

there is no --bb-edn flag

borkdude 2021-05-27T20:42:51.250900Z

I was citing kokada

lispyclouds 2021-05-27T20:42:58.251100Z

ah sorry i misread

lispyclouds 2021-05-27T20:43:22.251300Z

yeah i see the env var 😅

lispyclouds 2021-05-27T20:46:26.251500Z

would it be nice to add a cli opt for the edn file? most tools have it for a non standard path?

borkdude 2021-05-27T20:47:15.251700Z

I think there is a reason that deps.edn doesn't support this. Since you create ambiguity about :paths. Are they relative to the bb.edn file or the local dir?

borkdude 2021-05-27T20:47:39.251900Z

So I'd like to keep it under the radar for now maybe

kokada 2021-05-27T20:47:46.252100Z

Huh... It doesn't seem to work for me It does seems to load the :deps, but not the :paths

borkdude 2021-05-27T20:48:03.252300Z

exactly :P

lispyclouds 2021-05-27T20:48:17.252500Z

we can model it like docker? it uses -f for a non standard path to the Dockerfile and takes a context path

kokada 2021-05-27T20:48:53.252700Z

Well, I am quite happy with my solution if this is not possible

borkdude 2021-05-27T20:49:04.252900Z

I'm not even sure what the problem is, so right now: no :)

lispyclouds 2021-05-27T20:49:20.253100Z

😄

kokada 2021-05-27T20:49:46.253300Z

> we can model it like docker? it uses -f for a non standard path to the Dockerfile and takes a context path Yeah, this would be ideal for me

kokada 2021-05-27T20:49:55.253500Z

But if it isn't possible it seems fine too

borkdude 2021-05-27T20:50:25.253700Z

Perhaps you can lay out the issue in clear words in a Github discussion

borkdude 2021-05-27T20:50:37.253900Z

I am kind of overflown with Slack and Twitter threads right now

kokada 2021-05-27T20:50:37.254100Z

At least it is possible to manipulate the classpath on those cases

borkdude 2021-05-27T20:50:44.254300Z

and I will take a look at this later

lispyclouds 2021-05-27T20:50:51.254500Z

I can start it, maybe you can add to it @thiagokokada?

kokada 2021-05-27T20:50:55.254700Z

Ok

kokada 2021-05-27T20:51:16.254900Z

Thank you @rahul080327 @borkdude

borkdude 2021-05-27T20:51:17.255100Z

Note: please use discussions, not issues, so we can have some back and forth first

👍 1
1
lispyclouds 2021-05-27T20:51:53.255400Z

Should i add it to the bb tasks thread?

borkdude 2021-05-27T20:52:03.255600Z

Please start a new one

lispyclouds 2021-05-27T20:52:10.255800Z

got it

lispyclouds 2021-05-27T21:00:41.256100Z

https://github.com/babashka/babashka/discussions/869

borkdude 2021-05-27T21:04:42.256300Z

Can you remove the reference to BABASHKA_EDN? It's an internal detail, I think it's better if nobody finds it there and start using it

1
borkdude 2021-05-27T21:05:40.256500Z

So the way I'm reading that issue now. My initial response is: just write a bash wrapper script that cds into the right directory and then calls bb

borkdude 2021-05-27T21:20:00.257400Z

@arthur currently we don't have core.cache / core.memoize in babashka. not sure what the vanilla Clojure / Java answer to this should be

borkdude 2021-05-27T21:20:14.257700Z

perhaps just an atom with maps with timestamps

lispyclouds 2021-05-27T21:23:44.257900Z

Makes sense. I'm looking at it from the lens of a task runner like Gradle, make etc where we can specify the path to the conf, when not in pwd

borkdude 2021-05-27T21:25:33.258200Z

ok

2021-05-27T22:39:11.259100Z

There's a tiny error in the docs here https://babashka.org/fs/babashka.fs.html#var-delete. Think that should be (delete f) instead of dir.

borkdude 2021-05-28T07:32:19.264900Z

fixed