1 |
|
%% @doc Short functions useful for config file manipulations. |
2 |
|
%% This stuff can be pure, but most likely not. |
3 |
|
%% It's for generic functions. |
4 |
|
-module(mongoose_config_utils). |
5 |
|
-export([exit_or_halt/1, section_to_defaults/1, merge_sections/2]). |
6 |
|
|
7 |
|
-ignore_xref([section_to_defaults/1]). |
8 |
|
|
9 |
|
-include("mongoose_config_spec.hrl"). |
10 |
|
|
11 |
|
%% @doc If MongooseIM isn't yet running in this node, then halt the node |
12 |
|
-spec exit_or_halt(ExitText :: string()) -> none(). |
13 |
|
exit_or_halt(ExitText) -> |
14 |
:-( |
case [Vsn || {mongooseim, _Desc, Vsn} <- application:which_applications()] of |
15 |
|
[] -> |
16 |
:-( |
timer:sleep(1000), |
17 |
:-( |
halt(string:substr(ExitText, 1, 199)); |
18 |
|
[_] -> |
19 |
:-( |
exit(ExitText) |
20 |
|
end. |
21 |
|
|
22 |
|
section_to_defaults(#section{defaults = Defaults}) -> |
23 |
:-( |
Defaults. |
24 |
|
|
25 |
|
-spec merge_sections(mongoose_config_spec:config_section(), |
26 |
|
mongoose_config_spec:config_section()) -> |
27 |
|
mongoose_config_spec:config_section(). |
28 |
|
merge_sections(BasicSection, ExtraSection) -> |
29 |
7905 |
#section{items = Items1, required = Required1, defaults = Defaults1, |
30 |
|
process = Process1} = BasicSection, |
31 |
7905 |
#section{items = Items2, required = Required2, defaults = Defaults2, |
32 |
|
process = Process2} = ExtraSection, |
33 |
7905 |
BasicSection#section{items = maps:merge(Items1, Items2), |
34 |
|
required = Required1 ++ Required2, |
35 |
|
defaults = maps:merge(Defaults1, Defaults2), |
36 |
|
process = merge_process_functions(Process1, Process2)}. |
37 |
|
|
38 |
|
merge_process_functions(Process1, Process2) -> |
39 |
7905 |
fun(Path, V) -> |
40 |
5101 |
V1 = mongoose_config_parser_toml:process(Path, V, Process1), |
41 |
5101 |
mongoose_config_parser_toml:process(Path, V1, Process2) |
42 |
|
end. |