Fork me on GitHub
#babashka
<
2022-08-15
>
teodorlu09:08:23

Hi! I recall a borkdude/thanos meme posted (I think) on Twitter a while ago. Where the infinity stones were babashka/sci/nbb/... But I can't find the meme/image. Anyone got a pointer?

teodorlu09:08:15

Perfect, thank you!

nonrecursive21:08:48

hey y’all, sorry if this is obvious but how do i cd in babashka?

teodorlu07:08:53

I use this function in most of my bb scripts:

(defn bash [cmd]
  (str/trim (:out (clojure.java.shell/sh "bash" "-c" cmd))))
Then you can just write normal bash in it. cd in a subshell works as expected:
(bash "(cd subdir && pwd)")
Another example:
(->> (bash "ls **/play.edn | sed 's|/play.edn||g'")
     (str/split-lines)
     (map (fn [slug] {:slug slug})))
(https://github.com/teodorlu/play.teod.eu/blob/94481a20248b3fdd70a896c8d2bc964c964d0686/play.clj#L45-L48)

nonrecursive19:08:11

thank you!

🙌 1
cldwalker15:08:59

@nonrecursive Heyo. shell is a handy fn to use in bb scripts as it behaves much like a line in bash would i.e. prints to stdout+ stderr and fails when a non-zero exit code occurs. See https://github.com/babashka/babashka/wiki/Bash-and-Babashka-equivalents#change-directory for how to cdwith it

cldwalker15:08:34

nm. I see borkdude answered but recommmend the wiki page for the bashisms

nonrecursive16:08:15

@U08ALHZ2N I appreciate that! I thought I’d seen that before but couldn’t find it

😃 1
borkdude21:08:10

@nonrecursive This is not so obvious: in a JVM-based environment, you can't change directories

borkdude21:08:45

If you're looking to spawn a program within a new directory, you can use (shell {:dir "foo"} "the-program")

nonrecursive21:08:35

this did the trick. thank you so much!

nonrecursive21:08:05

thank you @borkdude! yes that looks like it will work, awesome

borkdude21:08:07

or a clojure process: (clojure {:dir "foo"} "-M:bar")

Charlot Shaw21:08:40

So, I am quite possibly missing something here. I’m trying to write a script in Babashka, and I’m wanting to do so in functional setup, IE, a bunch of functions tied together in a -main function, instead of an imperative series of calls. I don’t control how my script is invoked, aside from the shebang at the top of the file. Is there a way I can configure Babashka to call my main function when invoked via shebang, or do I need make a bash oneliner to pass through the command line args?

borkdude21:08:20

@charlot.shaw You can use (apply -main *command-line-args*) at the bottom of your file

borkdude21:08:59

if you don't want to execute this, e.g. when you eval the file in the REPL, you can surround this with:

(when (= *file* (System/getProperty "babashka.file")) ...)

teodorlu07:08:29

I've been trying to understand this too! (analogous to if __name__ == "__main__" in python) Am I right to assume *file* is the file path of the current file that's being executed (System/getProperty "babashka.file") is the path of the file that was invoked as "main" with eg bb script.clj ?

borkdude07:08:46

correct!

💯 1
❤️ 1
Charlot Shaw21:08:15

Exactly my next question, thank you.

Charlot Shaw21:08:25

This would likely be a good addition to the docs

borkdude21:08:43

If you have a good suggestion how to make that clearer, feel free to submit PRs

Charlot Shaw21:08:50

Sure, will do.

borkdude21:08:04

Woohoo, 1000 members in this channel. A milestone! Unfortunately I can't see who it was, but thanks!

Charlot Shaw21:08:20

Actually, that was me.

Charlot Shaw21:08:27

I joined to ask this question

Charlot Shaw21:08:47

Biggest Kiriban I’ve been in so far.

borkdude21:08:37

I can send you free stickers as a reward ;)

Charlot Shaw21:08:31

I’ll not say no to free stickers. My prior laptop had all my dev stickers, but it bit it and I’ve not had the opportunity to source more.

borkdude21:08:01

OK, please send me your address in a DM. It will take a while since I'm leaving for a vacation tomorrow

Charlot Shaw21:08:31

No worries, surprise free stickers at any pace is lovely.