Created
March 8, 2018 15:28
-
-
Save igrishaev/5d8592151e1f2194b2994073dcac226f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ___.shell | |
(:require [clojure.java.shell :as shell] | |
[___.error :refer [error!]])) | |
(defmacro with-env | |
"The same as `clojure.java.shell/with-sh-env` but updates | |
an existing ENV rather than substitute it completely." | |
[env & forms] | |
`(let [env-old# (into {} (System/getenv)) | |
env-new# (merge env-old# ~env)] | |
(shell/with-sh-env env-new# | |
~@forms))) | |
(defn sh | |
"The same as `clojure.java.shell/sh` but raises an exception | |
in case of non-zero exit code. Otherwise, returns the output string." | |
[& args] | |
(let [{:keys [exit out err]} (apply shell/sh args)] | |
(if (= exit 0) | |
out | |
(error! "Shell error: code %s, reason: %s" exit err)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment