lib: Use syntactic sugar
[babashka-scripts.git] / lib.clj
CommitLineData
1770a260 1; Common utility functions used by scripts
f1d839f5 2
67d5a15a
JN
3(ns lib
4 (:require [babashka.process :as p]
5 [org.httpkit.client :as http]
6 [clojure.java.io :as io]
76cc62f0 7 [clojure.string :refer [replace-first split-lines]])
67d5a15a 8 (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
fbc08850
JN
9
10(defn run-cmd
11 [command]
12 (->> command
13 p/process
14 :out
15 slurp))
9da8be80 16
f1d839f5
JN
17(defn extract-file-from-zip
18 [zip-file-name source destination]
19 (let [zip-file (io/file zip-file-name)
f1d839f5
JN
20 dest (io/file destination)
21 fs (FileSystems/newFileSystem (.toPath zip-file) nil)
22 file-in-zip (.getPath fs source (into-array String []))]
0b0f2aa3
JN
23 (Files/copy file-in-zip
24 (.toPath dest)
f1d839f5
JN
25 (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING]))))
26
0b0f2aa3
JN
27(defn expand-home
28 [s]
f1d839f5 29 (if (.startsWith s "~")
67d5a15a 30 (replace-first s "~" (System/getProperty "user.home"))
f1d839f5 31 s))
9da8be80
JN
32
33(defn download-binary
34 [url destination]
35 (io/copy (:body @(http/get url)) (io/file destination)))
76cc62f0
JN
36
37(defn unixify
38 "Make a function `f` behave like a UNIX shell command."
39 [f]
40 (when (= *file* (System/getProperty "babashka.file"))
41 (let [content (if (> (.available System/in) 0)
42 (slurp *in*)
43 (first *command-line-args*))]
30bd2f34
JN
44 (->> content
45 (split-lines)
46 (filter seq)
47 (run! f)))))