]> njoseph.me Git - babashka-scripts.git/blame_incremental - lib.clj
utils: Install deb from GitHub. Reduce scripts.
[babashka-scripts.git] / lib.clj
... / ...
CommitLineData
1; Common utility functions used by scripts
2
3(ns lib
4 (:require [babashka.process :as p]
5 [org.httpkit.client :as http]
6 [clojure.java.io :as io]
7 [clojure.string :refer [replace-first split-lines]])
8 (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
9
10(defn run-cmd
11 [command]
12 (->> command
13 p/process
14 :out
15 slurp))
16
17(defn extract-file-from-zip
18 [zip-file-name source destination]
19 (let [zip-file (io/file zip-file-name)
20 dest (io/file destination)
21 fs (FileSystems/newFileSystem (.toPath zip-file) nil)
22 file-in-zip (.getPath fs source (into-array String []))]
23 (Files/copy file-in-zip
24 (.toPath dest)
25 (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING]))))
26
27(defn expand-home
28 [s]
29 (if (.startsWith s "~")
30 (replace-first s "~" (System/getProperty "user.home"))
31 s))
32
33(defn download-binary
34 [url destination]
35 (io/copy (:body @(http/get url)) (io/file destination)))
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*))]
44 (->> content
45 (split-lines)
46 (filter seq)
47 (run! f)))))