Repository: juxt/aero
Pull request #8 (2016-03-24 18:24:45)

Added envf from joplin w/ tests and docs
With this we should be able to drop Aero into Joplin.


Patch:
diff --git a/README.md b/README.md
index 0d62b3e..dbc1204 100644
--- a/README.md
+++ b/README.md
@@ -10,15 +10,17 @@ A small library for explicit, intentful configuration.
 
 Add the following dependency to your `project.clj` file
 
-[![Clojars Project](http://clojars.org/aero/latest-version.svg)](http://clojars.org/aero)
-
-[![Build Status](https://travis-ci.org/juxt/aero.png)](https://travis-ci.org/juxt/aero)
+```clojure
+[aero "0.1.3"]
+```
 
 ## Status
 
 Please note that being a 0.x.y version indicates the provisional status
 of this library, and that features are subject to change.
 
+[![Build Status](https://travis-ci.org/juxt/aero.png)](https://travis-ci.org/juxt/aero)
+
 ## Getting started
 
 Create a file called `config.edn` containing the following
@@ -110,6 +112,16 @@ Use `#env` with a vector to provide a default if the environment variable doesn'
 {:password #env [PORT 8080]}
 ```
 
+### envf
+
+Like `#env`, `#envf` formats a string using `clojure.core/format` with environment
+variables specified in a vector. This is useful for building up things like
+connection strings.
+
+``` clojure
+{:url #envf ["jdbc:postgresql://psq-prod/prod?user=%s&password=%s" PROD_USER PROD_PASSWD]}
+```
+
 ### cond
 
 Use cond as a kind of reader conditional.
@@ -152,7 +164,7 @@ can specify multiple hostnames in a set.
 
 Aero supports user-defined tag literals. Just extend the `reader` multimethod.
 
-```
+```clojure
 (defmethod reader `mytag
  [{:keys [profile] :as opts} tag value]
   (if (= value :favorite)
@@ -247,13 +259,6 @@ Aero is built on Clojure's [edn](https://github.com/edn-format/edn).
 Aero is influenced by [nomad](https://github.com/james-henderson/nomad),
 but purposely avoids instance, environment and private config.
 
-## Acknowledgments
-
-Thanks to the following people for inspiration, contributions,
-feedback and suggestions.
-
-* Gardner Vickers
-
 ## Copyright & License
 
 The MIT License (MIT)
diff --git a/project.clj b/project.clj
index 7a82e75..5c22ed1 100644
--- a/project.clj
+++ b/project.clj
@@ -1,6 +1,6 @@
 ;; Copyright © 2015, JUXT LTD.
 
-(defproject aero "0.2.0"
+(defproject aero "0.1.5"
   :description "Light and fluffy configuration"
   :url "http://github.com/juxt/aero"
   :license {:name "The MIT License"
diff --git a/src/aero/core.clj b/src/aero/core.clj
index 00de5a4..fed91e1 100644
--- a/src/aero/core.clj
+++ b/src/aero/core.clj
@@ -15,6 +15,11 @@
   (cond (vector? value) (or (System/getenv (str (first value))) (second value))
         :otherwise (System/getenv (str value))))
 
+(defmethod reader 'envf
+  [opts tag [fmt & args]]
+  (apply format fmt
+         (map (partial reader nil 'env) args)))
+
 (defmethod reader 'cond
   [{:keys [profile]} tag value]
   (cond (contains? value profile) (clojure.core/get value profile)
@@ -36,12 +41,12 @@
   "Optional second argument is a map. Keys are :profile, indicating the
   profile for use with #cond"
   ([r {:keys [schema] :as opts}]
-   (let [config
-         (with-open [pr (java.io.PushbackReader. (io/reader r))]
-           (edn/read
-            {:eof nil
-             :default (partial reader (merge {:profile :default} opts))}
-            pr))]
+   (let [config (with-open [pr (java.io.PushbackReader. (io/reader r))]
+                  (edn/read
+                   {:eof nil
+                    :default (partial reader (merge {:profile :default
+                                                     :filepath (str r)} opts))}
+                   pr))]
      (when schema
        (s/validate schema config))
      config))


Test patch:
diff --git a/test/aero/config.edn b/test/aero/config.edn
index 00d119f..9a4e212 100644
--- a/test/aero/config.edn
+++ b/test/aero/config.edn
@@ -1,2 +1,4 @@
 {:greeting "Hello World!"
- :flavor #myflavor :favorite }
+ :flavor #myflavor :favorite
+ :dumb-term #envf ["Terminal is %s" TERM]
+ :smart-term #envf ["Terminal is %s" [NONE "smart"]]}
diff --git a/test/aero/core_test.clj b/test/aero/core_test.clj
index b175554..3ba42b8 100644
--- a/test/aero/core_test.clj
+++ b/test/aero/core_test.clj
@@ -41,3 +41,10 @@
 (deftest define-new-type-test
   (let [config (read-config "test/aero/config.edn")]
     (is (= :chocolate (:flavor config)))))
+
+(deftest envf-test
+  (let [config (read-config "test/aero/config.edn")]
+    (is (= (format "Terminal is %s" (System/getenv "TERM"))
+           (:dumb-term config)))
+    (is (= (format "Terminal is %s" "smart")
+           (:smart-term config)))))