Packages

Standalone command-line interface for hex.pm

Current section

Files

Jump to
hexer src hexer_config.erl
Raw

src/hexer_config.erl

-module(hexer_config).
-export([ update/2
, api_key/0
]).
%%------------------------------------------------------------------------------
%% API
%%------------------------------------------------------------------------------
-spec update(atom(), any()) -> ok.
update(Key, Value) ->
Config = read(),
NewConfig = lists:ukeymerge(1, [{Key, Value}], lists:keysort(1, Config)),
write(NewConfig).
-spec api_key() -> {ok, string()}.
api_key() ->
case lists:keyfind(key, 1, read()) of
{key, Key} -> {ok, Key};
_ -> throw(no_api_key)
end.
%%------------------------------------------------------------------------------
%% Internal Functions
%%------------------------------------------------------------------------------
-spec read() -> [proplists:property()].
read() ->
case file:consult(config_path()) of
{ok, Config} -> Config;
{error, enoent} -> []
end.
-spec write([proplists:property()]) -> ok.
write(Config) ->
ConfigEncoded = [[io_lib:print(KV), ".\n"] || KV <- Config],
ok = file:write_file(config_path(), iolist_to_binary(ConfigEncoded)).
-spec config_path() -> string().
config_path() -> "hexer.config".