utils: Install deb from GitHub. Reduce scripts.
authorJoseph Nuthalapati <njoseph@riseup.net>
Fri, 30 Apr 2021 07:18:07 +0000 (12:48 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Fri, 30 Apr 2021 07:18:07 +0000 (12:48 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
.zprint.edn [new file with mode: 0644]
README.md
install-deb.clj [deleted file]
utils.clj

diff --git a/.zprint.edn b/.zprint.edn
new file mode 100644 (file)
index 0000000..e3a0290
--- /dev/null
@@ -0,0 +1 @@
+{:style :community}
index 7fa32943b0d794a115e9266b54bc7a8b32a53284..a5c24b4c1f7fb6e4dd0669dae6e606fe512ba42f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -23,10 +23,19 @@ export BABASHKA_PRELOADS=$BABASHKA_PRELOADS"  (require '[utils :refer :all])"
 export PATH=$PATH:$HOME/dev/babashka-scripts
 ```
 
-Some useful aliases.
+Some useful aliases and functions.
 
 ``` sh
 alias gup="bb -e '(git-pull-rebase-branch)'"
+alias bbr="rlwrap bb --repl"
+
+function bb-wrap {
+  expr="($1 \"$2\")"
+  bb -e $expr
+}
+
+alias install-deb="bb-wrap install-deb"
+alias install-deb-gh="bb-wrap install-deb-from-GitHub"
 ```
 
 ## Organization
diff --git a/install-deb.clj b/install-deb.clj
deleted file mode 100755 (executable)
index 662886e..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-#! /usr/bin/env bb
-
-; Install a deb package from URL
-
-(require '[clojure.java.io :as io]
-         '[lib :refer [download-binary run-cmd unixify]])
-
-(defn- install-deb
-  [url]
-  (println "Downloading deb package...")
-  (download-binary url "package.deb")
-  (println "Installing...")
-  (println (run-cmd ["sudo" "apt" "install" "./package.deb"]))
-  (println "Cleaning up..")
-  (io/delete-file "package.deb")
-  (println "Done."))
-
-(unixify install-deb)
index c3cdf5ac50022c968bc99eee99e60f2ebd036196..a19a9aa63e63ac795cc22b5458e7f53eb5739ef8 100644 (file)
--- a/utils.clj
+++ b/utils.clj
@@ -1,6 +1,9 @@
 (ns utils
-  (:require [lib :refer [run-cmd]]
-            [clojure.string :as str]))
+  (:require [clojure.string :as str]
+            [clojure.java.io :as io]
+            [cheshire.core :as json]
+            [org.httpkit.client :as http]
+            [lib :refer [download-binary run-cmd]]))
 
 (defn git-pull-rebase-branch
   "Do git pull and rebase branch with master"
   (let [current-branch (str/trim (run-cmd ["git" "branch" "--show-current"]))]
     (print (run-cmd ["git" "pull" "--rebase"]))
     (when (not (contains? #{"master" "main"} current-branch))
-     (run! print
-      (map run-cmd
-       '[["git" "checkout" "master"]
-         ["git" "pull" "--rebase"]
-         ["git" "checkout" "-"]
-         ["git" "rebase" "master"]])))))
+      (run! print
+            (map run-cmd
+              '[["git" "checkout" "master"] ["git" "pull" "--rebase"]
+                ["git" "checkout" "-"] ["git" "rebase" "master"]])))))
 
 (defn run-seq
   "Run a sequence of shell commands on a sequence of arguments.
   Examples:
 
   1. Play and delete each video from a folder
-     ls | bb -i '(run-seq ["mpv" "rm"] *input*)'
+     ls | bb -i '(run-seq [\"mpv\" \"rm\"] *input*)'
   "
   [commands arguments]
   (doseq [argument arguments
           command commands]
     (println (run-cmd [command argument]))))
+
+(defn install-deb
+  "Install a Debian package given a direct URL to the .deb file."
+  [url]
+  (println "Downloading deb package...")
+  (download-binary url "package.deb")
+  (println "Installing...")
+  (println (run-cmd ["sudo" "apt" "install" "./package.deb"]))
+  (println "Cleaning up..")
+  (io/delete-file "package.deb")
+  (println "Done."))
+
+(defn install-deb-from-GitHub
+  "Install a Debian package given a GitHub URL."
+  [url]
+
+  (assert (.contains url "github.com"))
+
+  (letfn [(extract-GitHub-author-project
+            ;; Extracts a string like author/project from URL.
+            [url]
+            (str/join "/" (subvec (str/split url #"/") 3)))
+          (make-api-url ;; just some string interpolation
+            [author-project]
+            (str "https://api.github.com/repos/"
+                 author-project
+                 "/releases/latest"))
+          (get-assets ;; Download list of assets from GitHub
+            [api-url]
+            (:assets (json/parse-string
+                      (:body @(http/get api-url)) true)))
+          (find-url-to-deb ;; Find Debian package URL from assets
+            [assets]
+            (some #(if (str/ends-with? % "deb") %)
+                  (map #(:browser_download_url %) assets)))]
+
+    (let [author-project (extract-GitHub-author-project url)
+          api-url (make-api-url author-project)
+          assets (get-assets api-url)
+          deb-url (find-url-to-deb assets)]
+      ;; Download and install Debian package
+      (install-deb deb-url))))