From: Joseph Nuthalapati Date: Fri, 26 Feb 2021 14:59:39 +0000 (+0530) Subject: Allow piping arguments into scripts X-Git-Url: https://njoseph.me/gitweb/babashka-scripts.git/commitdiff_plain/76cc62f08f19febed3fe9025a8315c3486fcd214?ds=sidebyside Allow piping arguments into scripts Signed-off-by: Joseph Nuthalapati --- diff --git a/ebook-to-audio-book.clj b/ebook-to-audio-book.clj index 39da869..48eb115 100755 --- a/ebook-to-audio-book.clj +++ b/ebook-to-audio-book.clj @@ -4,7 +4,7 @@ (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 @@ -13,19 +13,22 @@ ;; 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) diff --git a/install-deb.clj b/install-deb.clj index c8990a8..11fd2ed 100755 --- a/install-deb.clj +++ b/install-deb.clj @@ -2,14 +2,17 @@ ; 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."))) + +(unixify install-deb) diff --git a/lib.clj b/lib.clj index 1f88070..fb67137 100644 --- 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] - [clojure.string :refer [replace-first]]) + [clojure.string :refer [replace-first split-lines]]) (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption])) (defn run-cmd @@ -33,3 +33,13 @@ (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)))))