merge-config.escript 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env escript
  2. %% This script reads up emqx.conf and split the sections
  3. %% and dump sections to separate files.
  4. %% Sections are grouped between CONFIG_SECTION_BGN and
  5. %% CONFIG_SECTION_END pairs
  6. %%
  7. %% NOTE: this feature is so far not used in opensource
  8. %% edition due to backward-compatibility reasons.
  9. -mode(compile).
  10. -define(APPS, ["emqx", "emqx_dashboard", "emqx_auth"]).
  11. main(_) ->
  12. {ok, BaseConf} = file:read_file("apps/emqx_conf/etc/emqx_conf.conf"),
  13. Cfgs = get_all_cfgs("apps/"),
  14. Conf = [
  15. merge(BaseConf, Cfgs),
  16. io_lib:nl()
  17. ],
  18. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", Conf),
  19. merge_desc_files().
  20. merge(BaseConf, Cfgs) ->
  21. Confs = [BaseConf | lists:map(fun read_conf/1, Cfgs)],
  22. infix(lists:filter(fun(I) -> iolist_size(I) > 0 end, Confs), [io_lib:nl()]).
  23. read_conf(CfgFile) ->
  24. case filelib:is_regular(CfgFile) of
  25. true ->
  26. {ok, Bin1} = file:read_file(CfgFile),
  27. string:trim(Bin1, both);
  28. false ->
  29. <<>>
  30. end.
  31. infix([], _With) -> [];
  32. infix([One], _With) -> [One];
  33. infix([H | T], With) -> [H, With, infix(T, With)].
  34. get_all_cfgs(Root) ->
  35. Apps0 = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  36. Apps1 = (Apps0 -- ?APPS) ++ lists:reverse(?APPS),
  37. Dirs = [filename:join([Root, App]) || App <- Apps1],
  38. lists:foldl(fun get_cfgs/2, [], Dirs).
  39. get_all_cfgs(Dir, Cfgs) ->
  40. Fun = fun(E, Acc) ->
  41. Path = filename:join([Dir, E]),
  42. get_cfgs(Path, Acc)
  43. end,
  44. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  45. get_cfgs(Dir, Cfgs) ->
  46. case filelib:is_dir(Dir) of
  47. false ->
  48. Cfgs;
  49. _ ->
  50. Files = filelib:wildcard("*", Dir),
  51. case lists:member("etc", Files) of
  52. false ->
  53. try_enter_child(Dir, Files, Cfgs);
  54. true ->
  55. EtcDir = filename:join([Dir, "etc"]),
  56. %% the conf name must start with emqx
  57. %% because there are some other conf, and these conf don't start with emqx
  58. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  59. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  60. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  61. end
  62. end.
  63. try_enter_child(Dir, Files, Cfgs) ->
  64. case lists:member("src", Files) of
  65. false ->
  66. Cfgs;
  67. true ->
  68. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  69. end.
  70. %% Merge English descriptions.
  71. %% other translations are downloaded in pre-compile.sh
  72. merge_desc_files() ->
  73. BaseConf = <<"">>,
  74. Cfgs0 = get_all_desc_files(),
  75. Conf = do_merge_desc_files(BaseConf, Cfgs0),
  76. OutputFile = "apps/emqx_dashboard/priv/desc.en.hocon",
  77. ok = filelib:ensure_dir(OutputFile),
  78. ok = file:write_file(OutputFile, Conf).
  79. do_merge_desc_files(BaseConf, Cfgs) ->
  80. lists:foldl(
  81. fun(CfgFile, Acc) ->
  82. case filelib:is_regular(CfgFile) of
  83. true ->
  84. {ok, Bin1} = file:read_file(CfgFile),
  85. [Acc, io_lib:nl(), Bin1];
  86. false ->
  87. Acc
  88. end
  89. end,
  90. BaseConf,
  91. Cfgs
  92. ).
  93. get_all_desc_files() ->
  94. Dir = filename:join(["rel", "i18n"]),
  95. Files = filelib:wildcard("*.hocon", Dir),
  96. lists:map(fun(Name) -> filename:join([Dir, Name]) end, Files).