From 085b1e92b2a2d442d76490e41f8d697a6ec704a9 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 14 Sep 2012 09:25:03 -0500 Subject: handle config providers slightly differently from other providers --- src/relcool.erl | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/relcool.erl') diff --git a/src/relcool.erl b/src/relcool.erl index 2284d29..8c9363c 100644 --- a/src/relcool.erl +++ b/src/relcool.erl @@ -60,7 +60,40 @@ run_relcool_process(State) -> fun() -> rcl_state:format(State) end), - ok. + run_providers(State). + +%% @doc for now the 'config' provider is special in that it generates the +%% providers used by the rest of the system. We expect the config provider to be +%% the first provider in the system. Once the config provider is run, we get the +%% providers again and run the rest of them (because they could have been +%% updated by the config process). +run_providers(State0) -> + [ConfigProvider | _] = rcl_state:providers(State0), + State1 = run_provider(ConfigProvider, State0), + + Providers = rcl_state:providers(State1), + case Providers of + [ConfigProvider | Rest] -> + %% IF the config provider is still the first provider do not run it + %% again just run the rest. + run_providers(Rest, State1); + _ -> + run_providers(Providers, State1) + end, + init:stop(0). + +run_providers(Providers, State0) -> + lists:foldl(fun run_provider/2, State0, Providers). + + +-spec run_provider(rcl_provider:t(), rcl_state:t()) -> rcl_state:t(). +run_provider(Provider, State0) -> + case rcl_provider:do(Provider, State0) of + {ok, State1} -> + State1; + E={error, _} -> + report_error(rcl_provider:format_error(Provider, E)) + end. -spec usage() -> ok. usage() -> @@ -73,7 +106,9 @@ report_error(Error) -> usage(), erlang:halt(127). --spec to_error({error, Reason::term()}) -> string(). +-spec to_error(string() | {error, Reason::term()}) -> string(). +to_error(String) when erlang:is_list(String) -> + String; to_error({error,{invalid_option_arg, Arg}}) -> case Arg of {goals, Goal} -> -- cgit v1.2.3