This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-14
Channels
- # adventofcode (29)
- # aws (3)
- # babashka (25)
- # beginners (13)
- # calva (4)
- # cherry (7)
- # cider (26)
- # clj-kondo (9)
- # clojure (88)
- # clojure-europe (21)
- # clojure-losangeles (3)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (11)
- # clojuredesign-podcast (2)
- # clojurescript (4)
- # cursive (10)
- # datalevin (1)
- # emacs (50)
- # gratitude (1)
- # honeysql (12)
- # hyperfiddle (19)
- # jobs-discuss (28)
- # kaocha (3)
- # lsp (53)
- # malli (4)
- # meander (3)
- # off-topic (48)
- # re-frame (11)
- # releases (2)
- # ring-swagger (2)
- # shadow-cljs (50)
- # squint (26)
- # tools-build (3)
- # tools-deps (8)
- # xtdb (4)
- # yamlscript (1)
I need to check whether some Linux command is available in the user's shell. Here is what I came up with:
(defn sh->out
[opts & args]
(-> (apply process/sh opts args)
:out))
(defn command-available? [command]
(when-not (= (or nil "")
(sh->out "command" "-v" command))
true))
(command-available? "wget") ;; => true
(command-available? "flippetyfloppetyfloop") ;; => nil (punning)
Is this a reasonable approach, or is there a better way to do that?which something
returns something not found
command -v something
returns nil
(or ""
in Babashka)
I suppose it's a bit easier to check for nil
or ""
than to parse the command output to look for
? :thinking_face:
Since we're on the topic. My Babashka script depends on https://learn.microsoft.com/en-us/cli/azure/install-azure-cli (the az
command). Does Babashka provide any cross-platform mechanisms to help install missing shell commands?
that might be the best, I think I prefer that 99% of the time over some script installing tools for me
Now that's clean!
(defn command-available? [command]
(when (file/which command) true))
I'm porting a 2K line shell script to Babashka, and that one function replaces a 28-line shell function.I made a bb hiccup example here with inline JS here: https://github.com/squint-cljs/squint/blob/main/examples/babashka/index.clj
Sharing snippet to parse and compare version numbers in case it's useful to others:
(defn sh-out->json [opts & args]
(-> (apply process/sh opts args)
:out
(json/parse-string true)))
(defn parse-version-num [version-str]
(->> (string/split version-str #"\.")
(mapv #(Integer/parseInt %))))
(defn compare-version-nums [num-1 num-2]
(compare (parse-version-num num-1)
(parse-version-num num-2)))
(defn az-required-version? [required-num]
(let [installed-num (-> (sh-out->json "az" "version" "--output" "json")
:azure-cli)]
(when (-> (compare-version-nums installed-num required-num)
(>= 0))
true)))
Improvement suggestions are welcome! I'm still learning Clojure.Thereโs a nice lib for this in case you need to support more edge cases, itโs called version-clj
It appears I've finally fallen into the Clojure trap of just writing my own instead of searching for libs ๐
Does a "TUI library" exist for Clojure that works well with Babashka? I don't necessarily need a full-fledged TUI with menus, colors, loading bars, etc. Just something that loops, prompting the user for a selection or freeform input, then does something with that input, prints user feedback, and repeats, sort of like a REPL.