-
Notifications
You must be signed in to change notification settings - Fork 25
/
tools.cljc
150 lines (128 loc) · 4.62 KB
/
tools.cljc
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(ns cprop.tools
(:require [clojure.string :as s]
[clojure.java.io :as io])
(:import java.io.File
java.io.StringReader
java.util.Properties))
(defn key->prop [k]
(-> k
name
(s/replace "-" "_")))
(defn key->env [k]
(-> (key->prop k)
(s/upper-case)))
(defn link [connect from [to value]]
(let [to (key->prop to)]
[(str from connect to) value]))
(defn- map->flat [m key->x connect]
(reduce-kv (fn [path k v]
(if (map? v)
(concat (map (partial link connect (key->x k))
(map->flat v key->x connect))
path)
(conj path [(key->x k) v])))
[] m))
(defn- map->props [m]
(map->flat m key->prop "."))
(defn- map->env [m]
(map->flat m key->env "__"))
(defn temp-file
([fname] (temp-file fname ".tmp"))
([fname ext]
(.getAbsolutePath
(java.io.File/createTempFile fname ext))))
(defn- map->x-file [m m->x prop->x {:keys [path create?]
:or {path (temp-file (str "cprops-" (System/currentTimeMillis) "-"))
create? true}}]
(let [fpath (apply str path)
x-file (reduce (fn [f prop]
(str f (prop->x prop) "\n"))
"" (m->x m))]
(if create?
(do
(spit fpath x-file)
fpath)
x-file)))
(defn to-prop [[k v]]
(str k "=" v))
(defn to-env [[k v]]
(str "export " k "=" v))
(defn map->props-file
([m]
(map->props-file m {}))
([m opts]
(map->x-file m map->props to-prop opts)))
(defn map->env-file
([m]
(map->env-file m {}))
([m opts]
(map->x-file m map->env to-env opts)))
(defn map->properties
"convert map to java.util.Properties preserving hierarchy"
[m]
(let [ps (Properties.)]
(->> (StringReader. (map->props-file m {:create? false}))
(.load ps))
ps))
(defn contains-in?
"checks whether the nested key exists in a map"
[m k-path]
(let [one-before (get-in m (drop-last k-path))]
(when (map? one-before) ;; in case k-path is "longer" than a map: {:a {:b {:c 42}}} => [:a :b :c :d]
(contains? one-before (last k-path)))))
;; author of "deep-merge-with" is Chris Chouser: https://github.com/clojure/clojure-contrib/commit/19613025d233b5f445b1dd3460c4128f39218741
(defn deep-merge-with
"Like merge-with, but merges maps recursively, appling the given fn
only when there's a non-map at a particular level.
(deepmerge + {:a {:b {:c 1 :d {:x 1 :y 2}} :e 3} :f 4}
{:a {:b {:c 2 :d {:z 9} :z 3} :e 100}})
-> {:a {:b {:z 3, :c 3, :d {:z 9, :x 1, :y 2}}, :e 103}, :f 4}"
[f & maps]
(apply
(fn m [& maps]
(if (every? map? maps)
(apply merge-with m maps)
(apply f maps)))
maps))
(defn merge-maps [& m]
(apply deep-merge-with (fn [_ v] v) m))
(defn cloak [m & paths]
(reduce (fn [am path]
(if (contains-in? am path)
(update-in am path (fn [k] "*******"))
am)) m paths))
(defn in-debug? []
(when-let [debug (System/getenv "DEBUG")]
(= (s/lower-case debug) "y")))
(defn with-echo [config resource path]
(when (in-debug?)
(if-not (empty? config)
(println (str "read config from " resource ": \"" path "\""))
(println (str "(!) read config from " resource ": \"" path "\", but it is empty"))))
config)
;; "home" and "expand-home" functions belong to https://github.com/Raynes/fs
(let [homedir (io/file (System/getProperty "user.home"))
usersdir (.getParent homedir)]
(defn home
"With no arguments, returns the current value of the `user.home` system
property. If a `user` is passed, returns that user's home directory. It
is naively assumed to be a directory with the same name as the `user`
located relative to the parent of the current value of `user.home`."
([] homedir)
([user] (if (empty? user) homedir (io/file usersdir user)))))
(defn expand-home
"If `path` begins with a tilde (`~`), expand the tilde to the value
of the `user.home` system property. If the `path` begins with a
tilde immediately followed by some characters, they are assumed to
be a username. This is expanded to the path to that user's home
directory. This is (naively) assumed to be a directory with the same
name as the user relative to the parent of the current value of
`user.home`."
[path]
(let [path (str path)]
(if (.startsWith path "~")
(let [sep (.indexOf path File/separator)]
(if (neg? sep)
(home (subs path 1))
(io/file (home (subs path 1 sep)) (subs path (inc sep)))))
(io/file path))))