emqx_plugins_cli.erl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2017-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_plugins_cli).
  17. -export([
  18. list/1,
  19. describe/2,
  20. ensure_installed/2,
  21. ensure_uninstalled/2,
  22. ensure_started/2,
  23. ensure_stopped/2,
  24. restart/2,
  25. ensure_disabled/2,
  26. ensure_enabled/3
  27. ]).
  28. -include_lib("emqx/include/logger.hrl").
  29. -define(PRINT(EXPR, LOG_FUN),
  30. print(NameVsn, fun() -> EXPR end(), LOG_FUN, ?FUNCTION_NAME)
  31. ).
  32. list(LogFun) ->
  33. LogFun("~ts~n", [to_json(emqx_plugins:list())]).
  34. describe(NameVsn, LogFun) ->
  35. case emqx_plugins:describe(NameVsn) of
  36. {ok, Plugin} ->
  37. LogFun("~ts~n", [to_json(Plugin)]);
  38. {error, Reason} ->
  39. %% this should not happen unless the package is manually installed
  40. %% corrupted packages installed from emqx_plugins:ensure_installed
  41. %% should not leave behind corrupted files
  42. ?SLOG(error, #{
  43. msg => "failed_to_describe_plugin",
  44. name_vsn => NameVsn,
  45. cause => Reason
  46. }),
  47. %% do nothing to the CLI console
  48. ok
  49. end.
  50. ensure_installed(NameVsn, LogFun) ->
  51. ?PRINT(emqx_plugins:ensure_installed(NameVsn), LogFun).
  52. ensure_uninstalled(NameVsn, LogFun) ->
  53. ?PRINT(emqx_plugins:ensure_uninstalled(NameVsn), LogFun).
  54. ensure_started(NameVsn, LogFun) ->
  55. ?PRINT(emqx_plugins:ensure_started(NameVsn), LogFun).
  56. ensure_stopped(NameVsn, LogFun) ->
  57. ?PRINT(emqx_plugins:ensure_stopped(NameVsn), LogFun).
  58. restart(NameVsn, LogFun) ->
  59. ?PRINT(emqx_plugins:restart(NameVsn), LogFun).
  60. ensure_enabled(NameVsn, Position, LogFun) ->
  61. ?PRINT(emqx_plugins:ensure_enabled(NameVsn, Position), LogFun).
  62. ensure_disabled(NameVsn, LogFun) ->
  63. ?PRINT(emqx_plugins:ensure_disabled(NameVsn), LogFun).
  64. to_json(Input) ->
  65. emqx_logger_jsonfmt:best_effort_json(Input).
  66. print(NameVsn, Res, LogFun, Action) ->
  67. Obj = #{
  68. action => Action,
  69. name_vsn => NameVsn
  70. },
  71. JsonReady =
  72. case Res of
  73. ok ->
  74. Obj#{result => ok};
  75. {error, Reason} ->
  76. Obj#{
  77. result => not_ok,
  78. cause => Reason
  79. }
  80. end,
  81. LogFun("~ts~n", [to_json(JsonReady)]).