Last active
August 29, 2015 13:56
-
-
Save jebberjeb/8970235 to your computer and use it in GitHub Desktop.
This file contains 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
(ns test-app.foo | |
(:require [clojure.java.io :as io]) | |
(:import [java.util Properties])) | |
;;dotenv (w/ precedence per Colin -- shell > .env.foo > .env) | |
(defn f->p | |
[f] | |
(let [file (io/as-file f)] | |
(when (.exists file) (doto (Properties.) (.load (io/input-stream file)))))) | |
(defn get-env | |
[] | |
(let [env-vars (System/getenv) | |
dotenv ".env" | |
dotenvfoo (str dotenv "." (get env-vars "ENVIRONMENT"))] | |
(->> [(f->p dotenv) (f->p dotenvfoo) env-vars] | |
(map (partial into {})) | |
(apply merge)))) | |
;;environs | |
(defn get-var | |
[m k] | |
(or (get m k) (throw (ex-info (str "env var " k " not set") {})))) | |
;; app init | |
(swap! env (partial get-var (get-env))) ;; maybe alter a root def instead? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use java.util.Properties to load the .env, .env.foo files since it knows how to parse name=val style properties expertly. merge things in the correct order of precedence, using nil where files don't exist.