diff --git a/doerg/src/net/deertopia/doerg/common.clj b/doerg/src/net/deertopia/doerg/common.clj index 64740b1..9a73d5c 100644 --- a/doerg/src/net/deertopia/doerg/common.clj +++ b/doerg/src/net/deertopia/doerg/common.clj @@ -3,7 +3,10 @@ [clojure.string :as str] [clojure.tools.logging :as l] [clojure.java.io :as io]) - (:import (java.io FilterInputStream))) + (:import (java.io FilterInputStream StringWriter InputStream + OutputStream PrintStream ByteArrayOutputStream + ByteArrayInputStream) + (java.nio.charset StandardCharsets))) (defn deref-with-timeout [process ms] (let [p (promise) @@ -20,16 +23,37 @@ :timed-out-after-milliseconds ms})) @p))) -#_ -(defn tee-to-logs [input output] - (Thread. (fn [] - (io/copy (l/log-stream :debug))))) +(defn tee-input-stream [sink input-stream] + (proxy [FilterInputStream] [input-stream] + (read + ([] + (let [c (proxy-super read)] + (when (not= c -1) + (.write sink c)) + c)) + ([^bytes bs] + (let [n (proxy-super read bs)] + (when (not= n -1) + (.write sink bs 0 n)) + n)) + ([^bytes bs off len] + (let [n (proxy-super read bs off len)] + (when (not= n -1) + (.write sink bs off n)) + n))) + (close [] + (try (proxy-super close) + (finally (.close sink)))))) -(defrecord TeeInputStream - [out] - FilterInputStream) - -(extend) +(comment + (with-open [sink (ByteArrayOutputStream.) + out (ByteArrayOutputStream.) + in (ByteArrayInputStream. (.getBytes "hello worms"))] + (io/copy (tee-input-stream sink in) out) + (def the-out out) + (def the-sink sink) + {:out out + :sink sink})) (defn invoke [opts & cmd] (l/info (str/join " " (cons "$" cmd)))