initial commit

This commit is contained in:
Gered 2016-06-12 12:57:49 -04:00
commit 8ecd0438c4
5 changed files with 119 additions and 0 deletions

18
.gitignore vendored Normal file
View file

@ -0,0 +1,18 @@
.DS_Store
/target
/classes
/checkouts
/out
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
/*.project
/*.classpath
/.settings/
*.iml
*.ipr
*.iws
.idea

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Gered King
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

20
README.md Normal file
View file

@ -0,0 +1,20 @@
# config
Simple Clojure library for managing application configuration stored in
an external file in EDN format.
## Leiningen
```clj
[gered/config "0.1"]
```
## Usage
FIXME
## License
Copyright © 2016 Gered King
Distributed under the the MIT License. See LICENSE for more details.

10
project.clj Normal file
View file

@ -0,0 +1,10 @@
(defproject gered/config "0.1"
:description "Management of application configuration stored in an external EDN file."
:url "https://github.com/gered/config"
:license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/tools.logging "0.3.1"]]
:profiles {:provided
{:dependencies [[org.clojure/clojure "1.8.0"]]}})

50
src/config/core.clj Normal file
View file

@ -0,0 +1,50 @@
(ns config.core
(:refer-clojure :exclude [load get])
(:require
[clojure.java.io :as io]
[clojure.edn :as edn]
[clojure.tools.logging :as log]))
(def default-options
{
; whether to log a warning whenever config.core/get is called to access config values
; which are undefined in the configuration
:log-undefined? true
; whether to throw an exception whenever config.core/get is called to access config values
; which are undefined in the configuration
:throw-undefined? false
})
(defn load-edn-config
[f]
(edn/read-string (slurp f)))
(defn load
"Loads and returns an EDN configuration stored in a file. If the file is not
specified tries to load from 'config.edn' in the current directory.
For supported options, see config.core/default-options."
{:arglists '([]
[options]
[f]
[f options])}
[& args]
(let [[f options] (if (map? (first args))
[nil (first args)]
args)]
{:options (merge default-options options)
:config (load-edn-config (or f "config.edn"))}))
(defn get
"Returns a value from a loaded configuration under the path ks. May log a warning
or throw an exception if the value is undefined in the configuration depending on
the options specified when the configuration was loaded."
[config & ks]
(let [options (:options config)
value (get-in (:config config) ks ::undefined)]
(if (= ::undefined value)
(do
(if (:log-undefined? options) (log/warn "Read of undefined configuration value" ks))
(if (:throw-undefined? options) (throw (ex-info (str "Read of undefined configuration value " ks) {:ks ks :config (:config config)}))))
value)))