]> njoseph.me Git - babashka-scripts.git/blob - lib.clj
70fef53590648bb52015649d4ad872c4bc326737
[babashka-scripts.git] / lib.clj
1 ; Common utility functions used by scripts
2
3 (import '[java.nio.file Files FileSystems CopyOption StandardCopyOption])
4
5 (require '[babashka.process :as p] '[org.httpkit.client :as http])
6
7 (defn run-cmd
8 [command]
9 (->> command
10 p/process
11 :out
12 slurp))
13
14 (defn extract-file-from-zip
15 [zip-file-name source destination]
16 (let [zip-file (io/file zip-file-name)
17 src (io/file source)
18 dest (io/file destination)
19 fs (FileSystems/newFileSystem (.toPath zip-file) nil)
20 file-in-zip (.getPath fs source (into-array String []))]
21 (Files/copy file-in-zip
22 (.toPath dest)
23 (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING]))))
24
25 (defn expand-home
26 [s]
27 (if (.startsWith s "~")
28 (clojure.string/replace-first s "~" (System/getProperty "user.home"))
29 s))
30
31 (defn download-binary
32 [url destination]
33 (io/copy (:body @(http/get url)) (io/file destination)))