Create and use namespace for lib.clj
[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]
7 [clojure.string :refer [replace-first]])
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)))