feat: export API to read cluster and local overrides

This commit is contained in:
Zaiming (Stone) Shi 2023-01-31 14:20:34 +01:00
parent b3e486041b
commit f6dafc20ea
1 changed files with 18 additions and 5 deletions

View File

@ -24,6 +24,7 @@
init_load/2, init_load/2,
init_load/3, init_load/3,
read_override_conf/1, read_override_conf/1,
read_override_confs/0,
delete_override_conf_files/0, delete_override_conf_files/0,
check_config/2, check_config/2,
fill_defaults/1, fill_defaults/1,
@ -326,9 +327,7 @@ init_load(SchemaMod, RawConf, Opts) when is_map(RawConf) ->
ok = save_schema_mod_and_names(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
%% Merge environment variable overrides on top %% Merge environment variable overrides on top
RawConfWithEnvs = merge_envs(SchemaMod, RawConf), RawConfWithEnvs = merge_envs(SchemaMod, RawConf),
ClusterOverrides = read_override_conf(#{override_to => cluster}), Overrides = read_override_confs(),
LocalOverrides = read_override_conf(#{override_to => local}),
Overrides = hocon:deep_merge(ClusterOverrides, LocalOverrides),
RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides), RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides),
RootNames = get_root_names(), RootNames = get_root_names(),
RawConfAll = raw_conf_with_default(SchemaMod, RootNames, RawConfWithOverrides, Opts), RawConfAll = raw_conf_with_default(SchemaMod, RootNames, RawConfWithOverrides, Opts),
@ -337,6 +336,12 @@ init_load(SchemaMod, RawConf, Opts) when is_map(RawConf) ->
save_to_app_env(AppEnvs), save_to_app_env(AppEnvs),
ok = save_to_config_map(CheckedConf, RawConfAll). ok = save_to_config_map(CheckedConf, RawConfAll).
%% @doc Read merged cluster + local overrides.
read_override_confs() ->
ClusterOverrides = read_override_conf(#{override_to => cluster}),
LocalOverrides = read_override_conf(#{override_to => local}),
hocon:deep_merge(ClusterOverrides, LocalOverrides).
%% keep the raw and non-raw conf has the same keys to make update raw conf easier. %% keep the raw and non-raw conf has the same keys to make update raw conf easier.
raw_conf_with_default(SchemaMod, RootNames, RawConf, #{raw_with_default := true}) -> raw_conf_with_default(SchemaMod, RootNames, RawConf, #{raw_with_default := true}) ->
Fun = fun(Name, Acc) -> Fun = fun(Name, Acc) ->
@ -599,8 +604,16 @@ load_hocon_file(FileName, LoadType) ->
case filelib:is_regular(FileName) of case filelib:is_regular(FileName) of
true -> true ->
Opts = #{include_dirs => include_dirs(), format => LoadType}, Opts = #{include_dirs => include_dirs(), format => LoadType},
{ok, Raw0} = hocon:load(FileName, Opts), case hocon:load(FileName, Opts) of
{ok, Raw0} ->
Raw0; Raw0;
{error, Reason} ->
throw(#{
msg => failed_to_load_conf,
reason => Reason,
file => FileName
})
end;
false -> false ->
#{} #{}
end. end.