aboutsummaryrefslogtreecommitdiffstats
path: root/src/rcl_prv_config.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rcl_prv_config.erl')
-rw-r--r--src/rcl_prv_config.erl72
1 files changed, 66 insertions, 6 deletions
diff --git a/src/rcl_prv_config.erl b/src/rcl_prv_config.erl
index ee1c770..60655b1 100644
--- a/src/rcl_prv_config.erl
+++ b/src/rcl_prv_config.erl
@@ -1,3 +1,19 @@
+%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*-
+%%% Copyright 2012 Erlware, LLC. All Rights Reserved.
+%%%
+%%% This file is provided to you under the Apache License,
+%%% Version 2.0 (the "License"); you may not use this file
+%%% except in compliance with the License. You may obtain
+%%% a copy of the License at
+%%%
+%%% http://www.apache.org/licenses/LICENSE-2.0
+%%%
+%%% Unless required by applicable law or agreed to in writing,
+%%% software distributed under the License is distributed on an
+%%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%%% KIND, either express or implied. See the License for the
+%%% specific language governing permissions and limitations
+%%% under the License.
%%%-------------------------------------------------------------------
%%% @author Eric Merritt <[email protected]>
%%% @copyright 2011 Erlware, LLC.
@@ -29,8 +45,12 @@ init(State) ->
%% populating the state as a result.
-spec do(rcl_state:t()) ->{ok, rcl_state:t()} | relcool:error().
do(State) ->
- ConfigFiles = rcl_state:config_files(State),
- lists:foldl(fun load_config/2, {ok, State}, ConfigFiles).
+ case rcl_state:config_file(State) of
+ undefined ->
+ search_for_dominating_config(State);
+ ConfigFile when erlang:is_list(ConfigFile) ->
+ load_config(ConfigFile, State)
+ end.
-spec format_error(Reason::term()) -> iolist().
format_error({consult, ConfigFile, Reason}) ->
@@ -42,11 +62,51 @@ format_error({invalid_term, Term}) ->
%%%===================================================================
%%% Internal Functions
%%%===================================================================
--spec load_config(file:filename(), {ok, rcl_state:t()} | relcool:error()) ->
+search_for_dominating_config({ok, Cwd}) ->
+ ConfigFile = filename:join(Cwd, "relcool.config"),
+ case ec_file:exists(ConfigFile) of
+ true ->
+ {ok, ConfigFile};
+ false ->
+ search_for_dominating_config(parent_dir(Cwd))
+ end;
+search_for_dominating_config({error, _}) ->
+ no_config;
+search_for_dominating_config(State0) ->
+ {ok, Cwd} = file:get_cwd(),
+ case search_for_dominating_config({ok, Cwd}) of
+ {ok, Config} ->
+ %% we need to set the root dir on state as well
+ {ok, RootDir} = parent_dir(Config),
+ State1 = rcl_state:root_dir(State0, RootDir),
+ load_config(Config, rcl_state:config_file(State1, Config));
+ no_config ->
+ {ok, State0}
+ end.
+
+%% @doc Given a directory returns the name of the parent directory.
+-spec parent_dir(Filename::string()) ->
+ {ok, DirName::string()} | {error, no_parent_dir}.
+parent_dir(Filename) ->
+ parent_dir(filename:split(Filename), []).
+
+%% @doc Given list of directories, splits the list and returns all dirs but the
+%% last as a path.
+-spec parent_dir([string()], [string()]) ->
+ {ok, DirName::string()} | {error, no_parent_dir}.
+parent_dir([_H], []) ->
+ {error, no_parent_dir};
+parent_dir([], []) ->
+ {error, no_parent_dir};
+parent_dir([_H], Acc) ->
+ {ok, filename:join(lists:reverse(Acc))};
+parent_dir([H | T], Acc) ->
+ parent_dir(T, [H | Acc]).
+
+
+-spec load_config(file:filename(), rcl_state:t()) ->
{ok, rcl_state:t()} | relcool:error().
-load_config(_, Err = {error, _}) ->
- Err;
-load_config(ConfigFile, {ok, State}) ->
+load_config(ConfigFile, State) ->
{ok, CurrentCwd} = file:get_cwd(),
ok = file:set_cwd(filename:dirname(ConfigFile)),
Result = case file:consult(ConfigFile) of