merge-config.escript 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. main(_) ->
  11. {ok, BaseConf} = file:read_file("apps/emqx_conf/etc/emqx_conf.conf"),
  12. Cfgs = get_all_cfgs("apps/"),
  13. Conf = lists:foldl(fun(CfgFile, Acc) ->
  14. case filelib:is_regular(CfgFile) of
  15. true ->
  16. {ok, Bin1} = file:read_file(CfgFile),
  17. [Acc, io_lib:nl(), Bin1];
  18. false -> Acc
  19. end
  20. end, BaseConf, Cfgs),
  21. ok = file:write_file("apps/emqx_conf/etc/emqx.conf.all", Conf).
  22. get_all_cfgs(Root) ->
  23. Apps = filelib:wildcard("*", Root) -- ["emqx_machine", "emqx_conf"],
  24. Dirs = [filename:join([Root, App]) || App <- Apps],
  25. lists:foldl(fun get_cfgs/2, [], Dirs).
  26. get_all_cfgs(Dir, Cfgs) ->
  27. Fun = fun(E, Acc) ->
  28. Path = filename:join([Dir, E]),
  29. get_cfgs(Path, Acc)
  30. end,
  31. lists:foldl(Fun, Cfgs, filelib:wildcard("*", Dir)).
  32. get_cfgs(Dir, Cfgs) ->
  33. case filelib:is_dir(Dir) of
  34. false ->
  35. Cfgs;
  36. _ ->
  37. Files = filelib:wildcard("*", Dir),
  38. case lists:member("etc", Files) of
  39. false ->
  40. try_enter_child(Dir, Files, Cfgs);
  41. true ->
  42. EtcDir = filename:join([Dir, "etc"]),
  43. %% the conf name must start with emqx
  44. %% because there are some other conf, and these conf don't start with emqx
  45. Confs = filelib:wildcard("emqx*.conf", EtcDir),
  46. NewCfgs = [filename:join([EtcDir, Name]) || Name <- Confs],
  47. try_enter_child(Dir, Files, NewCfgs ++ Cfgs)
  48. end
  49. end.
  50. try_enter_child(Dir, Files, Cfgs) ->
  51. case lists:member("src", Files) of
  52. false ->
  53. Cfgs;
  54. true ->
  55. get_all_cfgs(filename:join([Dir, "src"]), Cfgs)
  56. end.