Skip to content

Instantly share code, notes, and snippets.

@sogaiu

sogaiu/which.clj

Last active Jan 24, 2020
Embed
What would you like to do?
which - find binary on PATH accounting for PATHEXT on Windows - (JVM Clojure and babashka)
(require '[clojure.java.io :as cji])
(require '[clojure.string :as cs])
(defn which
[bin-name]
(let [paths (clojure.string/split (or (System/getenv "PATH") "")
(re-pattern (System/getProperty "path.separator")))
;; for windows
pathexts (clojure.string/split (or (System/getenv "PATHEXT") "")
(re-pattern (System/getProperty "path.separator")))]
;; adapted work by taylorwood
(first
(for [path (distinct paths)
pathext pathexts
:let [exe-file (clojure.java.io/file path (str bin-name pathext))]
:when (.exists exe-file)]
(.getAbsolutePath exe-file)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment