Fork me on GitHub
#babashka
<
2023-06-07
>
leifericf11:06:35

I naively thought I could do this to get some JSON from a shell command, and parse it:

(ns leifs-utils.core
  (:require [babashka.process :as process]
            [cheshire.core :as json]))

(defn get-projects [org-url]
  (-> (process/shell (str "az devops project list --org " org-url))
      (json/parse-string true)))

(process/shell "az devops project list --org ")
But that will result in the following error: java.lang.ClassCastException: babashka.process.Process cannot be cast to java.lang.String How can I get- and parse the JSON from the shell command, instead of outputting it in the terminal?

borkdude11:06:40

You have to use the :out key of the process return value

💡 2
borkdude11:06:34

(-> (process/shell {:out :string} (str "az devops project list --org " org-url))
      :out
      (json/parse-string true))

leifericf11:06:56

I thought maybe so, and tried that earlier, but then I got this error instead: java.lang.ClassCastException: java.lang.ProcessBuilder$NullInputStream cannot be cast to java.lang.String Here is the whole code snippet:

(ns leifs-utils.core
  (:require [babashka.process :as process]
            [cheshire.core :as json]))

(defn get-projects [org-url]
  (-> (process/shell (str "az devops project list --org " org-url))
      :out
      (json/parse-string true)))

(get-projects "")

borkdude11:06:36

This is because you don't have {:out :string} here, like in the previous snippet

😳 2
borkdude11:06:57

btw, you don't have to concatenate org-url with a space, just provide it as another argument:

(process/shell {:out :string} "az devops project list --org" org-url)

💡 2
leifericf11:06:17

Ahaaa, gotcha! I see now. And thanks for the tip about not needing to concat!

👍 2
Colin (fosskers)00:06:15

If you (-> (sh "foo") :out ...) it should "just work"

leifericf07:06:30

Nice! Thanks, @U058DHAN3UP! That'a a bit cleaner.

👍 2
Kent Bull01:07:22

Where does the sh alias come from for the (-> (sh… example?

Noah Bogart15:06:41

How do I run a -m main function from a task alongside :extra-paths?

Alex Miller (Clojure team)15:06:19

clj -M:your-alias -m AClass
?

Alex Miller (Clojure team)15:06:40

or you could put :main-opts into an alias

Noah Bogart15:06:05

i mean in babashka, lol

Alex Miller (Clojure team)15:06:06

sorry! didn't realize what channel I was in :)

😅 4
❤️ 6
borkdude15:06:51

@UEENNMX0T In tasks: just write {:task namespace/-main}

👍 2