Fork me on GitHub
#babashka
<
2021-11-18
>
Pieter Slabbert08:11:37

Is there a way to export environment variables in babashka?

borkdude09:11:38

Depends on the context. Can you give more information about your problem?

Pieter Slabbert09:11:37

I'm trying to replace a bash script that that performs some curl requests and puts the result in a environment variable. so blah.sh contains

export BLAH=$(curl )
then I call source blah.sh

borkdude09:11:15

ah I see! yeah, you cannot alter the bash environment from an invoked program, this is why you need source and this is no different with bb. so you can do: (println (str "export BLAH=" the-value)) and then source the printed result with bash. Alternatively you can launch a new shell with that environment variable set:

(babashka.tasks/shell {:extra-env {"BLAH" the-value}} "bash")

borkdude09:11:28

$ bb -e '(babashka.tasks/shell {:extra-env {"BLAH" "dude"}} "bash")'

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit .
bash-3.2$ echo $BLAH
dude
bash-3.2$ exit
exit

borkdude09:11:57

(or use zsh ;))

borkdude09:11:54

ah, you can use (System/getenv "SHELL") to launch the default shell

papachan11:11:48

Is there something ready to be used under babashka tasks with a grep find replace a specific word so it could be for example replacing all the namespace by the user's one?

borkdude11:11:43

Do you mean general find and replace in text or clojure-code specific things?

papachan11:11:25

yes exactly.

borkdude11:11:01

yes exactly which one?

papachan11:11:36

for example in my specific case i can just leave a src working wth a dummy namespace as a template, and the user can just change it using bb as its convenience.

borkdude11:11:15

@U1ZJ06MMK I recommend using (str/replace s "{{namespace}}" namespace) where s is (slurp "some-file.clj")

borkdude11:11:28

if you want more advanced templating you can use selmer which is included in babashka

papachan11:11:59

cool ! selmer could be a good thing

borkdude11:11:17

it's very good

papachan11:11:09

thanks for the guidance. i think i am very close to solve it. thanks again michael!

👍 1
awb9911:11:33

I want to use babashka tasks in CI pipelinne for clojure deps.edn projects. Now what I don't know is how to use the same scripts in multiple projects. I was thinking to publish a git gist and then somehow load them and execute them. Or perhaps put them into a jar? Can bababshka use any jar?

borkdude11:11:06

You can put your scripts in a library and then refer to those libraries using :local/root

borkdude11:11:15

or simply load them using load-file

awb9911:11:41

Load file will not work because I have multiple GitHub repos. I want to share between all of them.

borkdude11:11:45

or :git/url if you want to publish the code in a github repo

borkdude11:11:15

I do this for example with my github upload code. https://github.com/borkdude/gh-release-artifact

awb9911:11:25

So let's take a simple example: in all my projects I want to call cljfmt, with some cljfmt settings that I like.

awb9911:11:02

I guess I would still have to write a bababshka script for each project then

borkdude11:11:01

are you paying attention to what I said before? I feel like we're talking past each other. you can use a git library

borkdude11:11:23

how are you calling cljfmt?

borkdude11:11:26

are a JVM library?

awb9911:11:31

All good. Your bb.edn exports tasks from a git dependency. That's all I need. It was so obvious that I missed it at first. Thanks @U04V15CAJ

awb9911:11:03

Very elegant solution. So elegant that it took me a little to understand it. Ha ha

borkdude11:11:58

Ah well, it doesn't export tasks from a git dependency, but it imports functions from a git dependency which you can use within tasks.

borkdude11:11:14

You cannot import/export tasks, only namespaces + functions

awb9911:11:12

Got it. Amazing solution. Clojure everywhere! Code sharing in a simple way! Love it!

awb9913:11:47

{:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}} :main-opts ["-m" "antq.core"]} }

awb9913:11:54

struggling again 🙂

awb9913:11:13

I want to create "bb outdated"

awb9913:11:22

I am sort of struggling with this.

awb9913:11:56

this edn needs to be added to my deps.edn file before running clojoure -M:outdated

awb9913:11:11

as far as I can tell -Sdeps only allows an edn map,

awb9913:11:34

which would be ok, but then I have a big string in the ocnfig. It would ail when things are more complex.

borkdude13:11:37

@hoertlehner why don't you put that :outdated stuff in deps.edn?

borkdude13:11:55

and then in bb.edn you do (clojure "-M:outdated")

awb9913:11:11

this is where it is at the moment.

awb9913:11:23

but I would liek to do more customization to it in the future.

borkdude13:11:35

you want to pass arguments to it?

awb9913:11:17

:lint {:extra-deps {clj-kondo/clj-kondo {:mvn/version "RELEASE"}} :main-opts ["-m" "clj-kondo.main" "--fail-level" "error" "--lint" "--config" "cljkondo.edn"]}

awb9913:11:33

lint (shell "clojure" "-M:lint" "src" "profiles/oauth2" "profiles/webserver")

awb9913:11:46

the stuff with parameter for cljkondo I have solved in this way.

awb9913:11:08

I really would like to have one deps.edn file that contains all the build tools I use

awb9913:11:18

it is a huge list,

awb9913:11:58

and it is the same information in like 40 projects.

awb9913:11:12

if I have the configuration in a bb.edn file,

awb9913:11:19

then why have something else in deps.edn

borkdude13:11:22

@hoertlehner I think you can just add "-Sdeps" '{:deps ...}

borkdude13:11:56

Example:

{:tasks
 {clj-kondo (clojure "-Sdeps" '{:deps {clj-kondo/clj-kondo {:mvn/version "2021.10.19"}}}
                     "-M" "-m" "clj-kondo.main" "--lint" "src")}}

awb9914:11:18

You are the best!

awb9915:11:03

very stupid question: is there a babashka function to change the directory? It seems "cd" is not a binary, but a bash command. So I dont want to assume the user has bash, but stay in babashkas world.

borkdude15:11:55

@hoertlehner the working directory is immutable in the JVM / babashka. You can pass :dir "foo" to several task commands like shell and clojure. E.g.:

(shell {:dir "subproject"} "ls")

borkdude15:11:36

(clojure {:dir "subproject"} "-M:clj-kondo")