]> njoseph.me Git - babashka-scripts.git/blobdiff - lib.clj
utils: Install deb from GitHub. Reduce scripts.
[babashka-scripts.git] / lib.clj
diff --git a/lib.clj b/lib.clj
index 8978c3a2acf466c281022da8d72caaaf16a1bc91..16c905981142a16a07ea5ab119ffac68e361b57a 100644 (file)
--- a/lib.clj
+++ b/lib.clj
@@ -1,11 +1,22 @@
 ; Common utility functions used by scripts
 
-(import '[java.nio.file Files FileSystems CopyOption StandardCopyOption])
+(ns lib
+  (:require [babashka.process :as p]
+            [org.httpkit.client :as http]
+            [clojure.java.io :as io]
+            [clojure.string :refer [replace-first split-lines]])
+  (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
+
+(defn run-cmd
+  [command]
+  (->> command
+       p/process
+       :out
+       slurp))
 
 (defn extract-file-from-zip
   [zip-file-name source destination]
   (let [zip-file (io/file zip-file-name)
-        src (io/file source)
         dest (io/file destination)
         fs (FileSystems/newFileSystem (.toPath zip-file) nil)
         file-in-zip (.getPath fs source (into-array String []))]
 (defn expand-home
   [s]
   (if (.startsWith s "~")
-    (clojure.string/replace-first s "~" (System/getProperty "user.home"))
+    (replace-first s "~" (System/getProperty "user.home"))
     s))
+
+(defn download-binary
+  [url destination]
+  (io/copy (:body @(http/get url)) (io/file destination)))
+
+(defn unixify
+  "Make a function `f` behave like a UNIX shell command."
+  [f]
+  (when (= *file* (System/getProperty "babashka.file"))
+    (let [content (if (> (.available System/in) 0)
+                    (slurp *in*)
+                    (first *command-line-args*))]
+      (->> content
+           (split-lines)
+           (filter seq)
+           (run! f)))))