-
Notifications
You must be signed in to change notification settings - Fork 1
/
envset.clj
executable file
·46 lines (41 loc) · 1.22 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
#!/usr/bin/env bb
(ns envset
"Convert edn-based configs to env files."
(:require [clojure.edn :as edn]
[clojure.string :as str]))
(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")))
;; based on https://andersmurphy.com/2019/11/30/clojure-flattening-key-paths.html
(defn flatten-paths
"Convert a nested hashmap to path:value one."
([m path]
(lazy-seq
(when-let [[[k v] & xs] (seq m)]
(let [rest-flatten (flatten-paths xs path)
path* (conj path k)]
(if (and (map? v) (seq v))
(into (flatten-paths v path*) rest-flatten)
(cons [path* v] rest-flatten))))))
([m]
(into {} (flatten-paths m []))))
(comment
(->> (slurp "env.edn")
edn/read-string
flatten-paths
envmap->source
(spit "/tmp/env.sh")
))