-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
properties.clj
55 lines (49 loc) · 1.55 KB
/
properties.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
(ns migratus.properties
(:require
[clojure.string :as s]
[clojure.tools.logging :as log])
(:import
java.util.Date))
(def ^:private default-properties
#{"migratus.schema" "migratus.user" "migratus.database"})
(defn read-system-env []
(reduce
(fn [m [k v]]
(assoc m (-> (s/lower-case k) (s/replace "_" ".")) v))
{}
(System/getenv)))
(defn inject-properties [properties text]
(let [text-with-props (reduce
(fn [text [k v]]
(.replace text k (str v)))
text
properties)]
(doseq [x (re-seq #"\$\{[a-zA-Z0-9\-_\.]+}" text-with-props)]
(log/warn "no property found for key:" x))
text-with-props))
(defn system-properties
"read systme properties, accepts an optional collection of strings
specifying additional property names"
[property-names]
(let [props (read-system-env)]
(reduce
(fn [m k]
(if-let [v (get props k)]
(assoc m (str "${" k "}") v)
m))
{"${migratus.timestamp}" (.getTime (Date.))}
(into default-properties property-names))))
(defn map->props
([m] (map->props {} nil m))
([props path m]
(reduce
(fn [m [k v]]
(let [path (if path (str path "." (name k)) (name k))]
(if (map? v)
(map->props m path v)
(assoc m (str "${" path "}") v))))
props
m)))
(defn load-properties [{{:keys [env map]} :properties :as opts}]
(when (map? (:properties opts))
(merge (system-properties env) (map->props map))))