From: Joseph Nuthalapati Date: Fri, 30 Apr 2021 08:43:11 +0000 (+0530) Subject: unixify v2 X-Git-Url: https://njoseph.me/gitweb/babashka-scripts.git/commitdiff_plain/ead73d37568631a52a3759179a8fde48939d4afa unixify v2 Signed-off-by: Joseph Nuthalapati --- diff --git a/lib.clj b/lib.clj index 16c9059..d0f3b40 100644 --- a/lib.clj +++ b/lib.clj @@ -1,10 +1,11 @@ ; Common utility functions used by scripts +; Unlike the functions in utils.clj, these are not directly useful (ns lib (:require [babashka.process :as p] [org.httpkit.client :as http] [clojure.java.io :as io] - [clojure.string :refer [replace-first split-lines]]) + [clojure.string :as str]) (:import [java.nio.file Files FileSystems CopyOption StandardCopyOption])) (defn run-cmd @@ -27,7 +28,7 @@ (defn expand-home [s] (if (.startsWith s "~") - (replace-first s "~" (System/getProperty "user.home")) + (str/replace-first s "~" (System/getProperty "user.home")) s)) (defn download-binary @@ -35,13 +36,27 @@ (io/copy (:body @(http/get url)) (io/file destination))) (defn unixify - "Make a function `f` behave like a UNIX shell command." + "Make a function `f` behave like a UNIX shell command. + + Examples: + + # Multiple arguments + $ install-deb url1 url2 url3 + + # Read from file with one url on each line + $ install-deb `cat urls.txt` + + # Piping from another process + $ ls | install-deb # Note: install-deb doesn't work with ls + " [f] (when (= *file* (System/getProperty "babashka.file")) - (let [content (if (> (.available System/in) 0) - (slurp *in*) - (first *command-line-args*))] - (->> content - (split-lines) + (if (> (.available System/in) 0) + (->> (slurp *in*) + (str/split-lines) (filter seq) + (run! f)) + (->> (vec *command-line-args*) + (map #(str/split % #" ")) + (flatten) (run! f)))))