%%% Copyright (c) 2017, NORDUnet A/S. %%% See LICENSE for licensing information. -module(plopconfig). -export([get_env/1, get_env/2, init_table/0, load_config/0]). -define(PLOPCONFIG_TABLE, plopconfig). init_table() -> case ets:info(?PLOPCONFIG_TABLE) of undefined -> ok; _ -> ets:delete(?PLOPCONFIG_TABLE) end, ets:new(?PLOPCONFIG_TABLE, [set, public, named_table]), load_config(). keys() -> KeyList = lists:map(fun ({Key, _Value}) -> Key end, ets:tab2list(?PLOPCONFIG_TABLE)), sets:from_list(KeyList). load_config() -> {ok, Filename} = application:get_env(plop, plopconfig), {ok, File} = file:consult(Filename), OldKeys = keys(), lists:foreach( fun ({Key, Value}) -> true = ets:insert(?PLOPCONFIG_TABLE, {Key, Value}) end, hd(File)), NewKeys = keys(), RemovedKeys = sets:subtract(OldKeys, NewKeys), lists:foreach(fun (Key) -> ets:delete(?PLOPCONFIG_TABLE, Key) end, sets:to_list(RemovedKeys)), lager:info("loaded config: new keys ~p old keys ~p removed keys ~p", [sets:to_list(NewKeys), sets:to_list(OldKeys), sets:to_list(RemovedKeys)]), ok. get_env(Key, DefaultValue) -> case get_env(Key) of {ok, Value} -> Value; _ -> DefaultValue end. get_env(Key) -> case ets:lookup(?PLOPCONFIG_TABLE, Key) of [{_, Value}] -> {ok, Value}; [] -> undefined end.