/
core.clj
37 lines (31 loc) · 921 Bytes
/
core.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
(ns environ.core
(:require [clojure.string :as str]
[clojure.java.io :as io]))
(defn- keywordize [s]
(-> (str/lower-case s)
(str/replace "_" "-")
(str/replace "." "-")
(keyword)))
(defn- sanitize [k]
(let [s (keywordize (name k))]
(if-not (= k s) (println "Warning: environ key " k " has been corrected to " s))
s))
(defn- read-system-env []
(->> (System/getenv)
(map (fn [[k v]] [(keywordize k) v]))
(into {})))
(defn- read-system-props []
(->> (System/getProperties)
(map (fn [[k v]] [(keywordize k) v]))
(into {})))
(defn- read-env-file []
(let [env-file (io/file ".lein-env")]
(if (.exists env-file)
(into {} (for [[k v] (read-string (slurp env-file))]
[(sanitize k) v])))))
(defonce ^{:doc "A map of environment variables."}
env
(merge
(read-env-file)
(read-system-env)
(read-system-props)))