Skip to content

Instantly share code, notes, and snippets.

@rlm
Created January 6, 2011 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlm/768026 to your computer and use it in GitHub Desktop.
Save rlm/768026 to your computer and use it in GitHub Desktop.
(import '(java.io File BufferedReader ByteArrayOutputStream InputStreamReader))
(def println-repl (bound-fn* println))
(defn sh-seq
"same as sh but returns a lazy seq of the programs stdout, and prints all errors to the repl"
[& command+args]
(let [process (.exec (Runtime/getRuntime) (apply str (interpose " " command+args)))]
(let [reader (clojure.java.io/reader (.getInputStream process) :encoding "UTF-8")]
(line-seq reader))))
(defn sh-seq-debug
"same as sh-seq but prints all errors to the repl"
[& command+args]
(let [process (.exec (Runtime/getRuntime) (apply str (interpose " " command+args)))]
(let [reader (clojure.java.io/reader (.getInputStream process) :encoding "UTF-8")
error-reader (clojure.java.io/reader (.getErrorStream process) :encoding "UTF-8")]
(.start (Thread. #(dorun (map println-repl (line-seq error-reader)))))
(line-seq reader))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment