Fork me on GitHub
#babashka
<
2021-06-10
>
borkdude10:06:51

New babashka release: 0.4.5 Babashka proper: - Add java.net.InetSocketAddress - Add support for slingshot https://github.com/babashka/babashka/issues/675 - Add STM facilities (`dosync`, ref, etc.) - Fix .wait, .notify interop on arbitrary classes Deps.clj (used for resolving deps and clojure invocations): - Fix JVM option parsing https://github.com/borkdude/deps.clj/issues/46 Sci: various minor performance improvements. https://github.com/babashka/babashka/blob/master/CHANGELOG.md#045

babashka 13
❤️ 7
👍 2
🎉 2
emilaasa11:06:59

I'm a real noob at bb, and at running "imperative" commands from clj at all. How would you structure a pretty simple script that is supposed to do about this thing, here in zsh loop:

for i in `seq 1 100`; do
echo $i >> main.go
git add .
git commit -m $i
;done

borkdude11:06:42

@emilaasa for translates to doseq in clojure (used for side effects. seq 1 100 is (range 1 100) (not sure about inclusive/exclusive in bash). The echo call can be done using (spit "main.go" i :append true) Calling git can be done using shelling out e.g. with clojure.java.shell or babashka.process or in tasks using shell

emilaasa11:06:55

Thanks I'll try with doseq, I did reach for that but I think I messed up 🙂

borkdude11:06:33

Just use println when you're not sure, print things and you will figure it out

emilaasa11:06:19

Ah great it works exactly the same with waiting for shell output and everything!

(doseq [i (range 100)]
  (append-to-file "main.go" (gofunc (str "funfunc" i)))
  (gofmt "main.go")
  (add "main.go")
  (commit "asdf"))

emilaasa11:06:00

Thanks a lot - as normal with a good newbie question I feel stupid now 😄

borkdude11:06:31

no need, this channel is n00b question friendly!

emilaasa11:06:18

These implicit (or explicit let's be honest) have tripped me up before

Endre Bakken Stovner16:06:14

I am still unable to capture stderr in babashka/process:

(require '[babashka.process :as p :refer [process]]
           '[ :as io])
  (def c (process ["bash" "-c" "echo An error message > /dev/stderr"] {:err :inherit :shutdown p/destroy}))
  (with-open [rdr (io/reader (:out c))]
    (binding [*in* rdr]
      (loop []
        (let [line (read-line)]
          (println :line line)
          (when (not (nil? line))
            (recur))))))
prints
An error message
:line nil
I was expecting
:line An error message
:line nil
Any hints?

Endre Bakken Stovner16:06:21

Can you reproduce? This happens in both the rebl and the cider repls.

borkdude16:06:44

What are you expecting this example to do?

borkdude16:06:21

You are reading from the process's stdout, not stderr right

Endre Bakken Stovner16:06:38

But does not :inherit merge the two?

borkdude16:06:09

No, :inherit writes directly to the parent's corresponding stream

borkdude16:06:13

This works:

(require '[babashka.process :as p :refer [process]]
         '[ :as io])
(def c (process ["bash" "-c" "echo An error message > /dev/stderr"]
                {:shutdown p/destroy}))

(with-open [rdr (io/reader (:err c))]
  (binding [*in* rdr]
    (loop []
      (let [line (read-line)]
        (println :line line)
        (when (not (nil? line))
          (recur))))))

borkdude16:06:34

Note: I did not set :err :inherit and I read from :err, not :out

borkdude16:06:21

you could also do {:out *out* :err *out*}

borkdude16:06:35

or write to a custom stringwriter

Endre Bakken Stovner17:06:08

But it might be nice to be able to distinguish stderr and stdout also. Is there a way to read both at the same time? Thanks for the help btw!

borkdude17:06:22

yes, just start two such loops

🙏 2
Endre Bakken Stovner17:06:04

I will try. I'd imagine one would have to finish before the other begins, but I'll test it out.

borkdude17:06:45

well, you could run them side by side in a thread or so

Endre Bakken Stovner17:06:51

That is what I did 🙂 (.start (Thread. my-loop)). I'm used to multithreading stuff being such a pain in the behind. That seems to be one aspect of the language which is easier in Java than in Python

borkdude18:06:08

you can also do (future ..)