Replace babashka/curl with httpkit.client
[babashka-scripts.git] / update-babashka.clj
1 #! /usr/bin/env bb
2
3 (require '[clojure.java.shell :refer [sh]]
4 '[cheshire.core :as json]
5 '[org.httpkit.client :as http]
6 '[babashka.fs :as fs])
7
8 (defn babashka-latest-version
9 []
10 (-> @(http/get "https://api.github.com/repos/babashka/babashka/tags")
11 :body
12 (json/parse-string true)
13 first
14 :name
15 (subs 1)))
16
17 (defn get-download-url
18 [version architecture]
19 (str "https://github.com/babashka/babashka/releases/download/v"
20 version
21 "/babashka-"
22 version
23 "-"
24 architecture
25 ".zip"))
26
27 (defn check-latest
28 [version]
29 (when (= (System/getProperty "babashka.version") version)
30 (println "Already using the latest version.")
31 (System/exit 0)))
32
33 (defn download-latest
34 [version architecture output]
35 (let [url (get-download-url version architecture)]
36 (download-binary url output)))
37
38 (when (= *file* (System/getProperty "babashka.file"))
39 (let [architecture "linux-amd64" ;; TODO support multiple
40 ;; architectures
41 zip-file "babashka.zip"
42 destination (expand-home "~/bin/bb")
43 version (babashka-latest-version)]
44 (println (str "Latest version is " version))
45 (check-latest version)
46 (println "Updating...")
47 (download-latest version architecture zip-file)
48 (extract-file-from-zip zip-file "bb" destination)
49 (fs/set-posix-file-permissions destination "rwxr-xr-x")
50 (io/delete-file zip-file)
51 (println "Done!")))