-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvset.clj
executable file
·49 lines (43 loc) · 1.11 KB
/
envset.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
#!/usr/bin/env bb
(ns envset
"Convert edn-based configs to env files."
(:require [clojure.edn :as edn]
[clojure.string :as str]))
(defn flatten-paths
"Convert a nested hashmap to [path] value one."
([m]
(flatten-paths m []))
([m path]
(reduce-kv
(fn [acc k v]
(let [new-key (if path
(conj path k)
k)]
(if (map? v)
(merge acc (flatten-paths v new-key))
(assoc acc new-key v))))
{}
m)))
(defn- path->env-name [path]
(->> path
(map (comp #(str/replace % #"\-" "_") str/upper-case name))
(str/join "_")))
(defn- pp-value [value]
(cond
(vector? value) (str/join ":" value) ; PATH
(number? value) value
(string? value) (format "\"%s\"" value)
:else (format "'%s'/" (str value))))
(defn- envmap->source [m]
(->> m
(map (fn [[path value]]
(str (path->env-name path) "=" (pp-value value))))
sort
(str/join "\n")))
(comment
(->> (slurp "env.edn")
edn/read-string
flatten-paths
envmap->source
(spit "/tmp/env.sh")
))