From 8ecd0438c4b4cf194448813c9c439130b519ac7f Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 12 Jun 2016 12:57:49 -0400 Subject: [PATCH] initial commit --- .gitignore | 18 ++++++++++++++++ LICENSE | 21 +++++++++++++++++++ README.md | 20 ++++++++++++++++++ project.clj | 10 +++++++++ src/config/core.clj | 50 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 project.clj create mode 100644 src/config/core.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ada49b5 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0781dfc --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2bd1d2d --- /dev/null +++ b/README.md @@ -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. diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..2a98a7c --- /dev/null +++ b/project.clj @@ -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"]]}}) diff --git a/src/config/core.clj b/src/config/core.clj new file mode 100644 index 0000000..bd0e026 --- /dev/null +++ b/src/config/core.clj @@ -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)))