aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/shell_out.clj
diff options
context:
space:
mode:
authorStuart Halloway <stu@thinkrelevance.com>2009-01-25 21:28:01 +0000
committerStuart Halloway <stu@thinkrelevance.com>2009-01-25 21:28:01 +0000
commit522ee2fead85d24907cfc0247568658001b8c807 (patch)
tree347edc9587fae1a105a100ed96e4e923c0e0f1cd /src/clojure/contrib/shell_out.clj
parenta36aa739efe642715ebf1c669281afc9ede946c6 (diff)
added :dir and :env to shell-out
Diffstat (limited to 'src/clojure/contrib/shell_out.clj')
-rw-r--r--src/clojure/contrib/shell_out.clj70
1 files changed, 60 insertions, 10 deletions
diff --git a/src/clojure/contrib/shell_out.clj b/src/clojure/contrib/shell_out.clj
index 5bb7c97b..0e67c540 100644
--- a/src/clojure/contrib/shell_out.clj
+++ b/src/clojure/contrib/shell_out.clj
@@ -6,12 +6,27 @@
; the terms of this license.
; You must not remove this notice, or any other, from this software.
+; :dir and :env options added by Stuart Halloway
+
; Conveniently launch a sub-process providing to its stdin and
; collecting its stdout
(ns clojure.contrib.shell-out
(:import (java.io InputStreamReader OutputStreamWriter)))
+(def *sh-dir* nil)
+(def *sh-env* nil)
+
+(defmacro with-sh-dir [dir & forms]
+ "Sets the directory for use with sh, see sh for details."
+ `(binding [*sh-dir* ~dir]
+ ~@forms))
+
+(defmacro with-sh-env [env & forms]
+ "Sets the environment for use with sh, see sh for details."
+ `(binding [*sh-env* ~env]
+ ~@forms))
+
(defn- stream-seq
"Takes an InputStream and returns a lazy seq of integers from the stream."
[stream]
@@ -21,25 +36,60 @@
"Takes a seq of 'sh' arguments and returns a map of option keywords
to option values."
[args]
- (loop [[arg :as args] args opts {:cmd [] :out "UTF-8"}]
+ (loop [[arg :as args] args opts {:cmd [] :out "UTF-8" :dir *sh-dir* :env *sh-env*}]
(if-not args
opts
(if (keyword? arg)
(recur (rrest args) (assoc opts arg (second args)))
(recur (rest args) (update-in opts [:cmd] conj arg))))))
+(defn- as-env-key [arg]
+ "Helper so that callers can use symbols, keywords, or strings
+ when building an environment map."
+ (cond
+ (symbol? arg) (name arg)
+ (keyword? arg) (name arg)
+ (string? arg) arg))
+
+(defn- as-file [arg]
+ "Helper so that callers can pass a String for the :dir to sh."
+ (cond
+ (string? arg) (java.io.File. arg)
+ (nil? arg) nil
+ (instance? java.io.File arg) arg))
+
+(defn- as-env-string [arg]
+ "Helper so that callers can pass a Clojure map for the :env to sh."
+ (cond
+ (nil? arg) nil
+ (map? arg) (into-array String (map (fn [[k v]] (str (as-env-key k) "=" v)) arg))
+ true arg))
+
(defn sh
- "Passes the given strings to Runtime.exec() to launch a sub-process. An
- :in option may given followed by a String specifying text to be fed
- to the sub-process's stdin. An :out option may be given followed by
- :bytes or a String. If a String is given, it will be used as a
- character encoding name (for example \"UTF-8\" or \"ISO-8859-1\") to
- convert the sub-process's stdout to a String which is returned. If
- :bytes is given, the sub-process's stdout will be stored in a byte
- array and returned."
+ "Passes the given strings to Runtime.exec() to launch a sub-process.
+
+ Options are
+
+ :in may given followed by a String specifying text to be fed to the
+ sub-process's stdin.
+ :out option may be given followed by :bytes or a String. If a String
+ is given, it will be used as a character encoding name (for
+ example \"UTF-8\" or \"ISO-8859-1\") to convert the
+ sub-process's stdout to a String which is returned.
+ If :bytes is given, the sub-process's stdout will be stored in
+ a byte array and returned.
+ :env override the process env with a map (or the underlying Java
+ String[] if you are masochist).
+ :dir override the process dir with a String or java.io.File.
+
+ You can bind :env or :dir for multiple operations using with-sh-env
+ end with-sh-dir."
[& args]
(let [opts (parse-args args)
- proc (.exec (Runtime/getRuntime) (into-array (:cmd opts)))
+ proc (.exec (Runtime/getRuntime)
+ (into-array (:cmd opts))
+ (as-env-string (:env opts))
+ (as-file (:dir opts)))
in-stream (.getInputStream proc)]
(when (:in opts)
(with-open [osw (OutputStreamWriter. (.getOutputStream proc))]