]> njoseph.me Git - babashka-scripts.git/commitdiff
Allow piping arguments into scripts
authorJoseph Nuthalapati <njoseph@riseup.net>
Fri, 26 Feb 2021 14:59:39 +0000 (20:29 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Fri, 26 Feb 2021 14:59:39 +0000 (20:29 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
ebook-to-audio-book.clj
install-deb.clj
lib.clj

index 39da869ed28aff7630f3ebac1b1d483e218ddbc2..48eb115c18717fe9311f6cdfe865ca83d11931d8 100755 (executable)
@@ -4,7 +4,7 @@
 
 (require '[clojure.java.io :as io]
          '[clojure.string :refer [split]]
 
 (require '[clojure.java.io :as io]
          '[clojure.string :refer [split]]
-         '[lib :refer [run-cmd extract-file-from-zip]])
+         '[lib :refer [run-cmd extract-file-from-zip unixify]])
 
 ;; TODO Check if all required utilities are installed
 
 
 ;; TODO Check if all required utilities are installed
 
 
 ;; TODO Use festival-tts or say depending on OS
 
 
 ;; TODO Use festival-tts or say depending on OS
 
-(let [book-file (first *command-line-args*)
-      title (first (split book-file #"\."))
-      txtz-file (str title ".txtz")
-      txt-file (str title ".txt")
-      aiff-file (str title ".aiff")
-      mp3-file (str title ".mp3")]
-  (println "Converting to text...")
-  (run-cmd ["ebook-convert" book-file txtz-file])
-  (extract-file-from-zip txtz-file "index.txt" txt-file)
-  (println "Converting text to audio. Don't hold your breath!")
-  (run-cmd ["say" "-f" txt-file "-o" aiff-file])
-  (println "Creating mp3 file...")
-  (run-cmd ["ffmpeg" "-i" aiff-file mp3-file])
-  (println "Cleaning up...")
-  (run! io/delete-file [txtz-file txt-file aiff-file])
-  (println (str "Done! Check out " mp3-file)))
+(defn- convert
+  [book-file]
+  (let [title (first (split book-file #"\."))
+        txtz-file (str title ".txtz")
+        txt-file (str title ".txt")
+        aiff-file (str title ".aiff")
+        mp3-file (str title ".mp3")]
+    (println "Converting to text...")
+    (run-cmd ["ebook-convert" book-file txtz-file])
+    (extract-file-from-zip txtz-file "index.txt" txt-file)
+    (println "Converting text to audio. Don't hold your breath!")
+    (run-cmd ["say" "-f" txt-file "-o" aiff-file])
+    (println "Creating mp3 file...")
+    (run-cmd ["ffmpeg" "-i" aiff-file mp3-file])
+    (println "Cleaning up...")
+    (run! io/delete-file [txtz-file txt-file aiff-file])
+    (println (str "Done! Check out " mp3-file))))
+
+(unixify convert)
index c8990a84785333cc242cd5cbe898b1f46bc9e5fb..11fd2ed6661b1269919ea0379286364abf7def2e 100755 (executable)
@@ -2,14 +2,17 @@
 
 ; Install a deb package from URL
 
 
 ; Install a deb package from URL
 
-(require '[clojure.java.io :as io] '[lib :refer [download-binary run-cmd]])
+(require '[clojure.java.io :as io]
+         '[lib :refer [download-binary run-cmd unixify]])
 
 
-(when (= *file* (System/getProperty "babashka.file"))
-  (let [url (first *command-line-args*)]
-    (println "Downloading deb package...")
+(defn- install-deb
+  [url]
+  (println "Downloading deb package..."
     (download-binary url "package.deb")
     (println "Installing...")
     (run-cmd ["sudo" "gdebi" "--non-interactive" "package.deb"])
     (println "Cleaning up..")
     (io/delete-file "package.deb")
     (println "Done.")))
     (download-binary url "package.deb")
     (println "Installing...")
     (run-cmd ["sudo" "gdebi" "--non-interactive" "package.deb"])
     (println "Cleaning up..")
     (io/delete-file "package.deb")
     (println "Done.")))
+
+(unixify install-deb)
diff --git a/lib.clj b/lib.clj
index 1f88070ba456a7b9e516567a17180a9c3a8cf26c..fb67137a1fb0045a221222a63431e74c99c2fe88 100644 (file)
--- a/lib.clj
+++ b/lib.clj
@@ -4,7 +4,7 @@
   (:require [babashka.process :as p]
             [org.httpkit.client :as http]
             [clojure.java.io :as io]
   (:require [babashka.process :as p]
             [org.httpkit.client :as http]
             [clojure.java.io :as io]
-            [clojure.string :refer [replace-first]])
+            [clojure.string :refer [replace-first split-lines]])
   (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
 
 (defn run-cmd
   (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption]))
 
 (defn run-cmd
 (defn download-binary
   [url destination]
   (io/copy (:body @(http/get url)) (io/file destination)))
 (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*))]
+      (doseq [arg (filter #(not= "" %) (split-lines content))]
+        (f arg)))))