Skip to content

Instantly share code, notes, and snippets.

@taylorSando
Created January 20, 2015 19:39
Show Gist options
  • Save taylorSando/4bbbe8217ddb2863ae8c to your computer and use it in GitHub Desktop.
Save taylorSando/4bbbe8217ddb2863ae8c to your computer and use it in GitHub Desktop.
(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