Skip to content

Commit f3ee354

Browse files
committed
Apply comments
1 parent fa35a3c commit f3ee354

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

apps/els_core/src/els_config.erl

+35-11
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,43 @@ consult_config([], ReportMissingConfig) ->
242242
{undefined, #{}};
243243
consult_config([Path | Paths], ReportMissingConfig) ->
244244
?LOG_INFO("Reading config file. path=~p", [Path]),
245-
try consult_file(Path) of
246-
[] -> {Path, #{}};
247-
[Config] -> {Path, Config};
248-
{ok, Config} -> {Path, maps:from_list(Config)};
249-
{error, Reason} ->
250-
?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p"
251-
, [Path, error, Reason]),
252-
consult_config(Paths, ReportMissingConfig)
245+
Result =
246+
case filename:extension(Path) of
247+
".yaml" ->
248+
try_yaml(Path);
249+
".config" ->
250+
try_eterm(Path, _TryYaml = true)
251+
end,
252+
case Result of
253+
{ok, Config} ->
254+
{Path, Config};
255+
{error, Class, Error} ->
256+
?LOG_WARNING("Could not read config file: path=~p class=~p error=~p",
257+
[Path, Class, Error]),
258+
consult_config(Paths, ReportMissingConfig)
259+
end.
260+
261+
-spec try_yaml(path()) -> {ok, map()} | {error, atom(), term()}.
262+
try_yaml(Path) ->
263+
try yamerl:decode_file(Path, [{map_node_format, map}]) of
264+
[] ->
265+
{ok, #{}};
266+
[Config] ->
267+
{ok, Config}
253268
catch
254269
Class:Error ->
255-
?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p"
256-
, [Path, Class, Error]),
257-
consult_config(Paths, ReportMissingConfig)
270+
{error, Class, Error}
271+
end.
272+
273+
-spec try_eterm(path(), boolean()) -> {ok, map()} | {error, atom(), term()}.
274+
try_eterm(Path, TryYaml) ->
275+
case consult_file(Path) of
276+
{ok, Config} ->
277+
{ok, maps:from_list(Config)};
278+
{error, _} when TryYaml ->
279+
try_yaml(Path);
280+
{error, Reason} ->
281+
{error, error, Reason}
258282
end.
259283

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

0 commit comments

Comments
 (0)