-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
52 lines (44 loc) · 1.74 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns resource-config.core
(:require [clojure.core.memoize :as memo]
[aero.core :as a]
[clojure.edn :as edn]
[clojure.java.io :as io]
[resource-config.data-readers])
(:import (java.io FileNotFoundException PushbackReader)))
(def config-file-name "config.edn")
(def read-config
(memo/memo
(fn [config-file]
(if-let [file (io/resource config-file)]
(a/read-config file {:profile (keyword (System/getenv "AERO_PROFILE"))})
(throw (java.io.FileNotFoundException.
(str "Config file " config-file " not found in resource paths.")))))))
(defn reload-config! []
(memo/memo-clear! read-config [config-file-name]))
(defn config
"Read a value from the configuration file at `keys` (`keys` is a
path of keys as used in `get-in`). There are two arities.
If the file does not exist, throws a FileNotFoundException.
If a value at `keys` does exist, returns it.
If a value at `keys` does not exist and no `default` is given (single
argument), throws an exception.
If a value at `keys` does not exist and a `default` is given,
returns the default."
([] (config []))
([keys]
(let [config (read-config config-file-name)
val (get-in config keys ::not-found)]
(when (= ::not-found val)
(throw (ex-info
(str "Hey! I went looking for these keys ("
(pr-str keys)
") in the config "
"and I couldn't find them.")
{:keys keys
:config config
:config-uri (-> config-file-name
io/resource
str)})))
val))
([keys default]
(get-in (read-config config-file-name) keys default)))