Skip to content

Commit

Permalink
Apply comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sirikid committed Dec 25, 2021
1 parent b0b7713 commit a9cbbb4
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions apps/els_core/src/els_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,43 @@ consult_config([], ReportMissingConfig) ->
{undefined, #{}};
consult_config([Path | Paths], ReportMissingConfig) ->
?LOG_INFO("Reading config file. path=~p", [Path]),
try consult_file(Path) of
[] -> {Path, #{}};
[Config] -> {Path, Config};
{ok, Config} -> {Path, maps:from_list(Config)};
{error, Reason} ->
?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p"
, [Path, error, Reason]),
consult_config(Paths, ReportMissingConfig)
Result =
case filename:extension(Path) of
".yaml" ->
try_yaml(Path);
".config" ->
try_eterm(Path, _TryYaml = true)
end,
case Result of
{ok, Config} ->
{Path, Config};
{error, Class, Error} ->
?LOG_WARNING("Could not read config file: path=~p class=~p error=~p",
[Path, Class, Error]),
consult_config(Paths, ReportMissingConfig)
end.

-spec try_yaml(path()) -> {ok, map()} | {error, atom(), term()}.
try_yaml(Path) ->
try yamerl:decode_file(Path, [{map_node_format, map}]) of
[] ->
{ok, #{}};
[Config] ->
{ok, Config}
catch
Class:Error ->
?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p"
, [Path, Class, Error]),
consult_config(Paths, ReportMissingConfig)
{error, Class, Error}
end.

-spec try_eterm(path(), boolean()) -> {ok, map()} | {error, atom(), term()}.
try_eterm(Path, TryYaml) ->
case consult_file(Path) of
{ok, Config} ->
{ok, maps:from_list(Config)};
{error, _} when TryYaml ->
try_yaml(Path);
{error, Reason} ->
{error, error, Reason}
end.

-spec consult_file(path()) -> [map()] | {ok, [term()]} | {error, term()}.
Expand Down

0 comments on commit a9cbbb4

Please sign in to comment.