fix(config): don't write to override.conf if 'override_conf_file' is not set

This commit is contained in:
Shawn 2021-09-02 20:20:14 +08:00
parent daca99f0f6
commit a89bc97ed8
1 changed files with 15 additions and 9 deletions

View File

@ -258,12 +258,15 @@ init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
init_load(SchemaMod, RawConf0) when is_map(RawConf0) -> init_load(SchemaMod, RawConf0) when is_map(RawConf0) ->
ok = save_schema_mod_and_names(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
%% override part of the input conf using emqx_override.conf %% override part of the input conf using emqx_override.conf
RawConf = maps:merge(RawConf0, maps:with(maps:keys(RawConf0), read_override_conf())), RawConf = merge_with_override_conf(RawConf0),
%% check and save configs %% check and save configs
{_AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf), {_AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf),
ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf), ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf),
maps:with(get_root_names(), RawConf)). maps:with(get_root_names(), RawConf)).
merge_with_override_conf(RawConf) ->
maps:merge(RawConf, maps:with(maps:keys(RawConf), read_override_conf())).
-spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf}
when AppEnvs :: app_envs(), CheckedConf :: config(). when AppEnvs :: app_envs(), CheckedConf :: config().
check_config(SchemaMod, RawConf) -> check_config(SchemaMod, RawConf) ->
@ -348,13 +351,16 @@ save_to_config_map(Conf, RawConf) ->
save_to_override_conf(undefined) -> save_to_override_conf(undefined) ->
ok; ok;
save_to_override_conf(RawConf) -> save_to_override_conf(RawConf) ->
FileName = emqx_override_conf_name(), case emqx_override_conf_name() of
undefined -> ok;
FileName ->
ok = filelib:ensure_dir(FileName), ok = filelib:ensure_dir(FileName),
case file:write_file(FileName, jsx:prettify(jsx:encode(RawConf))) of case file:write_file(FileName, jsx:prettify(jsx:encode(RawConf))) of
ok -> ok; ok -> ok;
{error, Reason} -> {error, Reason} ->
logger:error("write to ~s failed, ~p", [FileName, Reason]), logger:error("write to ~s failed, ~p", [FileName, Reason]),
{error, Reason} {error, Reason}
end
end. end.
load_hocon_file(FileName, LoadType) -> load_hocon_file(FileName, LoadType) ->
@ -366,7 +372,7 @@ load_hocon_file(FileName, LoadType) ->
end. end.
emqx_override_conf_name() -> emqx_override_conf_name() ->
application:get_env(emqx, override_conf_file, "emqx_override.conf"). application:get_env(emqx, override_conf_file, undefined).
do_get(Type, KeyPath) -> do_get(Type, KeyPath) ->
Ref = make_ref(), Ref = make_ref(),