Permalink
Browse files

Initial commit

  • Loading branch information...
Devin Torres
Devin Torres committed Apr 3, 2012
0 parents commit 6a97fb180db605dd3273a40f566e3e0401846ba6
Showing with 165 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +28 −0 Makefile
  3. +26 −0 README.md
  4. +24 −0 UNLICENSE
  5. BIN rebar
  6. +3 −0 rebar.config
  7. +6 −0 src/confit.app.src
  8. +63 −0 src/confit.erl
  9. +4 −0 src/confit_parser.erl
  10. +8 −0 src/confit_term_parser.erl
@@ -0,0 +1,3 @@
+.dialyzer_plt
+ebin/
+deps/
@@ -0,0 +1,28 @@
+REBAR = ./rebar
+DIALYZER = dialyzer
+
+DIALYZER_WARNINGS = -Wunmatched_returns -Werror_handling \
+ -Wrace_conditions -Wunderspecs
+
+.PHONY: all compile test clean get-deps build-plt dialyze
+
+all: compile
+
+compile:
+ @$(REBAR) compile
+
+test: compile
+ @$(REBAR) eunit skip_deps=true
+
+clean:
+ @$(REBAR) clean
+
+get-deps:
+ @$(REBAR) get-deps
+
+build-plt:
+ @$(DIALYZER) --build_plt --output_plt .dialyzer_plt \
+ --apps kernel stdlib
+
+dialyze: compile
+ @$(DIALYZER) --src src --plt .dialyzer_plt $(DIALYZER_WARNINGS)
@@ -0,0 +1,26 @@
+Confit - A configuration preserver
+==================================
+
+Confit allows you to parse and store configuration values for your
+applications to use during runtime.
+
+Usage
+-----
+
+```erlang
+confit:start_link(foo, "foo.config"),
+Username = confit:get(foo, database, username)
+```
+
+### Parsing other configuration formats
+
+Create a module to parse your configuration format and make sure it exports
+a function `parse_file/1` that returns `{ok, Proplist}`. The `confit_parser`
+behaviour specifies the interface. See `confit_term_parser.erl`.
+
+For example, to use [Zucchini](https://github.com/devinus/zucchini):
+
+```erlang
+confit:start_link(foo, zucchini, "foo.ini"),
+Username = confit:get(foo, database, username)
+```
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+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 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.
+
+For more information, please refer to <http://unlicense.org/>
BIN rebar
Binary file not shown.
@@ -0,0 +1,3 @@
+{erl_opts, [warnings_as_errors]}.
+{eunit_opts, [verbose]}.
+{cover_enabled, true}.
@@ -0,0 +1,6 @@
+{application, confit, [
+ {description, "A configuration preserver"},
+ {vsn, "0.1.0"},
+ {applications, [kernel, stdlib]},
+ {registered, []}
+]}.
@@ -0,0 +1,63 @@
+-module(confit).
+-behaviour(gen_server).
+
+-export([get/1, get/2, get/3, get/4, start_link/2, start_link/3]).
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
+ code_change/3]).
+
+-record(state, {config}).
+
+-spec get(Name :: atom()) -> term().
+get(Name) ->
+ gen_server:call(Name, get).
+
+-spec get(Name :: atom(), Secton :: term()) -> term().
+get(Name, Section) ->
+ gen_server:call(Name, {get, Section}).
+
+-spec get(Name :: atom(), Secton :: term(), Key :: term()) -> term().
+get(Name, Section, Key) ->
+ get(Name, Section, Key, undefined).
+
+-spec get(Name :: atom(), Section :: term(), Key :: term(), Default :: term())
+ -> term().
+get(Name, Section, Key, Default) ->
+ gen_server:call(Name, {get, Section, Key, Default}).
+
+-spec start_link(Name :: atom(), ConfigFile :: string()) -> {ok, pid()}.
+start_link(Name, ConfigFile) ->
+ start_link(Name, confit_term_parser, ConfigFile).
+
+-spec start_link(Name :: atom(), Parser :: module(), ConfigFile :: string())
+ -> {ok, pid()}.
+start_link(Name, Parser, ConfigFile) ->
+ gen_server:start_link({local, Name}, ?MODULE, [Parser, ConfigFile], []).
+
+init([Parser, ConfigFile]) ->
+ {ok, Config} = Parser:parse_file(ConfigFile),
+ {ok, #state{config=Config}}.
+
+handle_call(get, _From, #state{config=Config}=State) ->
+ {reply, Config, State};
+handle_call({get, Section}, _From, #state{config=Config}=State) ->
+ Reply = proplists:get_value(Section, Config),
+ {reply, Reply, State};
+handle_call({get, Section, Key, Default}, _From, #state{config=Config}=State) ->
+ SectionConfig = proplists:get_value(Section, Config),
+ Reply = proplists:get_value(Key, SectionConfig, Default),
+ {reply, Reply, State};
+handle_call(_Request, _From, State) ->
+ {reply, ok, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+terminate(_Reason, _Status) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
@@ -0,0 +1,4 @@
+-module(confit_parser).
+
+-callback parse_file(Filename :: string())
+ -> {ok, [{term(), term()}, ...]}.
@@ -0,0 +1,8 @@
+-module(confit_term_parser).
+
+-behaviour(confit_parser).
+
+-export([parse_file/1]).
+
+parse_file(Filename) ->
+ file:consult(Filename).

0 comments on commit 6a97fb1

Please sign in to comment.