This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-14
Channels
- # babashka (43)
- # beginners (47)
- # biff (1)
- # calva (16)
- # cider (7)
- # clerk (6)
- # clj-kondo (39)
- # cljdoc (49)
- # clojure (29)
- # clojure-brasil (1)
- # clojure-europe (93)
- # clojure-losangeles (1)
- # clojure-norway (34)
- # conjure (7)
- # datalevin (8)
- # events (1)
- # gratitude (3)
- # honeysql (6)
- # hyperfiddle (2)
- # introduce-yourself (1)
- # javascript (1)
- # jobs-discuss (9)
- # lsp (3)
- # malli (10)
- # off-topic (8)
- # pedestal (3)
- # rewrite-clj (2)
- # shadow-cljs (17)
- # sql (33)
- # vim (1)
- # xtdb (31)
I have written this function to find all Git repositories within a given directory:
(defn find-repos
[search-path]
(->> (babashka.fs/glob search-path "**.git" {:hidden true})
(pmap #(str %))
(pmap #(clojure.string/replace-first % #"/\.git$" ""))
(doall)))
The regular expression removes the "filename part" of the path (`/.git`).
While this works, it looks ugly, and I suspect it's not the best approach to use regular expressions to mess with the paths.
Does babashka.fs
have a function to get a path without the filename at the end?
For context, the Bash script I'm replacing does this to execute Git commands in the found directories:
find . -name ".git" -maxdepth 2 -type d -execdir git pull \; -execdir git status \;
Notice the use of -execdir
as opposed to -exec
.
I have written another function to execute a Git command within a directory:
(defn run-git-command
[path command]
(-> (process/sh {:dir path} "git" command)
:out))
My plan is to combine find-repos
with run-git-command
to do the same. That's why I need the path without the filename.Does babashka.fs/parent
do what you want?
https://github.com/babashka/fs/blob/master/API.md#babashka.fs/parent
Ahaaa, yeah! It looks like it. I missed that because I didn't think to look for the word "parent" in the docs 😅
I'm quickly learning that one of my greatest weaknesses is not knowing what to search for and how to best navigate docs 😅
> I'm quickly learning that one of my greatest weaknesses is not knowing what to search for and how to best navigate docs 😅 > Not sure if I agree! I mostly know these things because Borkdude has been helping me out with stupid questions. Some things are hard to learn on one's own! I think asking "how would you ... ?" and "can I get feedback on this code?" questions is a great way to learn :)
I also think it's fair to just work with strings when you don't know the specific functions that could solve the task with less code. See for instance this thing I wrote: https://github.com/teodorlu/play.teod.eu/blob/42e9d3edc9dfe6958c2c87cbc2bde73d6c9f97fb/play.clj#L55 At that time, I wasn't familiar with babashka fs at all!
Also as a side note, I’d recommend against the use of pmap pretty much everywhere, specially if you’re seeing the need to use a doall after it. pmap though is easy doesn’t really add much value and is almost always not a good choice when using with side effects.
This particular code the overhead from managing the threads and the laziness would probably defeat the gains that you’re looking for 🙂
Thanks for the advice, @U7ERLH6JX! When I use map
it doesn't process the whole collection, so I had to use run!
or doseq
or pmap
+ doall
. What would be the best approach in this scenario?
The problem with pmap is that it’s lazy and has chunking too like map. If you wanna do things in parallel and not care about laziness pmap is okay hence ruling out side effects. This thread should help: https://clojurians.slack.com/archives/C03S1KBA2/p1678968328528409
bb supports the nice executor framework that the JDK has and it’s a bit more involved but simpler solution. Also works nicely with the snappy virtual threads 😄
I’m seeing some strange behavior with connecting over a socket repl. Details in thread:
I’m starting bb with
❯ bb --socket-repl 5000
Babashka socket REPL started at 127.0.0.1:5000
Babashka v1.3.176 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.
user=>
and I can connect with netcat:
❯ netcat localhost 5000
Babashka v1.3.176 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.
user=>
Note this is over localhostif i connect with inf-clojure on localhost 5000, i’m never seeing any output like a repl prompt. If i send stuff over localhost i see
(+ 1 1)
RTSP/1.0 403 Forbidden
Content-Length: 0
Server: AirTunes/675.4.1
but it connects to 127.0.0.1, perhaps "localhost" does something slightly different, not sure
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ControlCe 2829 borkdude 21u IPv4 0x7e0ffdd84f48315 0t0 TCP *:commplex-main (LISTEN)
ControlCe 2829 borkdude 22u IPv6 0x7e0ffe24fd6c5cd 0t0 TCP *:commplex-main (LISTEN)
when using babahska.process
, can I stream the process output to *out*
? using *out*
for :out
in the option map appears to only redirect the final output of the script after it completes, not while it is running.
This is due to buffering. But there's already a stream by default with process/process
and you can just read from that manually
You can also use {:out :inherit}
if you don't actually want to capture it, but see it as it's written
by default, :inherit
goes to stdout of where my REPL was launched (which is a terminal window). can I get the same behavior as :inherit
but for my REPL connection?