Created
October 30, 2021 10:22
-
-
Save a13/3c2146b46928f911b5ea1629e9a81eda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(ns envset | |
(:require [clojure.edn :as edn] | |
[clojure.string :as str])) | |
(defn read-env-config [name] | |
(->> (slurp name) | |
(edn/read-string))) | |
(defn collect-leaves [path values] | |
(if (map? values) | |
(mapcat (fn [[k v]] | |
(collect-leaves (cons k path) v)) | |
values) | |
(vector [(reverse path) values]))) | |
(defn path->env-name [path] | |
(->> path | |
(map (comp #(str/replace % #"\-" "_") str/upper-case name)) | |
(str/join "_"))) | |
(defn pp-value [value] | |
(cond | |
(number? value) value | |
(string? value) (format "'%s'" value) | |
:else (format "'%s'" (str value)))) | |
(defn envmap->source [m] | |
(->> m | |
(collect-leaves []) | |
(map (fn [[path value]] | |
;; FIXME: join vectors with : | |
(str (path->env-name path) "=" (pp-value value)))) | |
(str/join "\n"))) | |
(->> "env.edn" | |
read-env-config | |
envmap->source | |
;; (spit "/tmp/env.sh") | |
println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment