/
utils.clj
66 lines (54 loc) · 2.36 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns dirac.lib.utils
(:require [clojure.core.async :refer [<! <!! >!! alts!! chan close! go go-loop put! timeout]]
[clojure.pprint :refer [pprint]]
[clojure.string :as string]
[env-config.core :as env-config]))
(defn get-nrepl-server-url [host port]
(str "nrepl://" host ":" port))
(defn get-ws-url [host port]
(str "ws://" host ":" port))
(defn pp [data & [level length]]
(with-out-str
(binding [*print-level* (or level 5) ; we have to be careful here, data might contain circular references
*print-length* (or length 200)]
(pprint data))))
(defn first-part [s]
(first (string/split s #"-" 2)))
(defn sid [thing]
(str "#" (first-part (cond
(map? thing) (str (:id thing))
(seq? thing) (str (first thing))
:else (str thing)))))
(defn get-env-vars []
(-> {}
(into (System/getenv))
(into (System/getProperties))))
(defn read-env-config [prefix]
(env-config/make-config-with-logging prefix (get-env-vars)))
(defn deep-merge-ignoring-nils
"Recursively merges maps. If keys are not maps, the last value wins. Nils are ignored."
[& vals]
(let [non-nil-vals (remove nil? vals)]
(if (every? map? non-nil-vals)
(apply merge-with deep-merge-ignoring-nils non-nil-vals)
(last non-nil-vals))))
(defn get-status-set [message]
(let [status (:status message)]
(into #{} status)))
(defn wait-for-all-responses! [responses-channel]
(loop []
(when (<!! responses-channel)
(recur))))
(defn exit-with-error! [msg & [exit-code]]
(binding [*out* *err*]
(println "-----------------------------------------------------------------------------------------------------------")
(println "ERROR!")
(println msg)
(println "-----------------------------------------------------------------------------------------------------------")
(System/exit (or exit-code 1))))
(defn print-warning! [& args]
(binding [*out* *err*]
(println "-----------------------------------------------------------------------------------------------------------")
(println "WARNING!")
(apply println args)
(println "-----------------------------------------------------------------------------------------------------------")))