diff options
author | Alexander Taggart <alex.taggart@expojure.com> | 2011-03-25 18:35:38 -0700 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2011-05-06 09:46:30 -0400 |
commit | 39d973926f1b34332a9c766115f48a71f956b332 (patch) | |
tree | 67a9089d5e52a0217a6c24cce7a3848823432405 | |
parent | 601d9521f88f8fb00e670d2823857cdcb2b2e1c3 (diff) |
clojure.java.shell/sh accepts as input anything that clojure.java.io/copy does.
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | src/clj/clojure/java/shell.clj | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/clj/clojure/java/shell.clj b/src/clj/clojure/java/shell.clj index 5388e7a2..5331c6d2 100644 --- a/src/clj/clojure/java/shell.clj +++ b/src/clj/clojure/java/shell.clj @@ -12,7 +12,7 @@ collecting its stdout"} clojure.java.shell (:use [clojure.java.io :only (as-file copy)]) - (:import (java.io OutputStreamWriter ByteArrayOutputStream StringWriter) + (:import (java.io InputStream File Reader OutputStreamWriter ByteArrayOutputStream StringWriter) (java.nio.charset Charset))) (def ^:dynamic *sh-dir* nil) @@ -81,8 +81,8 @@ collecting its stdout"} Options are - :in may be given followed by a String or byte array specifying input - to be fed to the sub-process's stdin. + :in may be given followed by an InputStream, Reader, File, byte[], or + String input to be fed to the sub-process's stdin. :in-enc option may be given followed by a String, used as a character encoding name (for example \"UTF-8\" or \"ISO-8859-1\") to convert the input string specified by the :in option to the @@ -110,17 +110,14 @@ collecting its stdout"} [& args] (let [[cmd opts] (parse-args args) proc (.exec (Runtime/getRuntime) - ^"[Ljava.lang.String;" (into-array cmd) - (as-env-strings (:env opts)) - (as-file (:dir opts))) + ^"[Ljava.lang.String;" (into-array cmd) + (as-env-strings (:env opts)) + (as-file (:dir opts))) {:keys [in in-enc out-enc]} opts] (if in (future - (if (instance? (class (byte-array 0)) in) - (with-open [os (.getOutputStream proc)] - (.write os ^"[B" in)) - (with-open [osw (OutputStreamWriter. (.getOutputStream proc) ^String in-enc)] - (.write osw ^String in)))) + (with-open [os (.getOutputStream proc)] + (copy in os :encoding in-enc))) (.close (.getOutputStream proc))) (with-open [stdout (.getInputStream proc) stderr (.getErrorStream proc)] @@ -134,6 +131,7 @@ collecting its stdout"} (println (sh "ls" "-l")) (println (sh "ls" "-l" "/no-such-thing")) (println (sh "sed" "s/[aeiou]/oo/g" :in "hello there\n")) +(println (sh "sed" "s/[aeiou]/oo/g" :in (java.io.StringReader. "hello there\n"))) (println (sh "cat" :in "x\u25bax\n")) (println (sh "echo" "x\u25bax")) (println (sh "echo" "x\u25bax" :out-enc "ISO-8859-1")) ; reads 4 single-byte chars |