-
Notifications
You must be signed in to change notification settings - Fork 7
/
env.clj
48 lines (43 loc) · 1.23 KB
/
env.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
(ns org.purefn.kurosawa.config.env
"Load configuration from environment variables."
(:require [clojure.string :as str]
[org.purefn.kurosawa.config.parse :as parse]
[taoensso.timbre :as log]))
(defn- munge-name
[s]
(str/join
"-"
(-> (str/lower-case s)
(str/split #"_")
(rest))))
(defn- env-variables
[]
(->> (System/getenv)
(map (juxt (comp str/lower-case key)
(comp munge-name key)
val))
(group-by (comp first #(str/split % #"_") first))
(into {})))
(defn fetch
"Fetches a map from environment variables according to the convention:
* MYSQL_USER=root
* MYSQL_PASSWORD=secure
* MYSQL_NUM_THREADS=10
> (fetch)
{mysql
{user root
password secure
num-threads 10}}"
[]
(log/info "Reading config from environment variables")
(->> (System/getenv)
(map (juxt (comp str/lower-case key)
(comp munge-name key)
val))
(group-by (comp first #(str/split % #"_") first))
(map (fn [[k kvs]]
[k (->> (map (juxt second
(comp parse/value last))
kvs)
(into {}))]))
(into {})))