Created
January 20, 2015 19:39
-
-
Save taylorSando/4bbbe8217ddb2863ae8c 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 path.to.services.env | |
(:require | |
[clojure.tools.logging :as log] | |
[puppetlabs.trapperkeeper.core :as tk] | |
[puppetlabs.trapperkeeper.services :as s])) | |
(defprotocol EnvironmentConfigService | |
(get-config [this] | |
"Returns a map containing all of the configuration values (enviornmentally aware). Any string value that is | |
prefixed by ENV_, will be considered an environmental variable.") | |
(get-in-config [this ks] [this ks default] | |
"Returns the individual configuration value from the nested | |
configuration structure, where ks is a sequence of keys. | |
Returns nil if the key is not present, or the default value if | |
supplied.")) | |
(defn- envify-map [m] | |
(clojure.walk/postwalk | |
(fn [x] | |
(if (string? x) | |
(if-let [[_ env] (re-find #"ENV_(.*+)" x)] | |
(System/getenv env) | |
x) | |
x)) | |
m)) | |
(tk/defservice service | |
EnvironmentConfigService | |
[[:ConfigService get-config]] | |
(init [this context] | |
(assoc context :env-config (envify-map (get-config)))) | |
(get-config [this] | |
(let [context (s/service-context this)] | |
(:env-config context))) | |
(get-in-config [this ks] | |
(let [context (s/service-context this)] | |
(get-in (:env-config context) ks))) | |
(get-in-config [this ks default] | |
(let [context (s/service-context this)] | |
(get-in (:env-config context) ks default)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment