Merge pull request #8198 from ieQu1/list-rule-cli

feat(rule_engine): CLI to list rules
This commit is contained in:
ieQu1 2022-06-13 16:05:22 +02:00 committed by GitHub
commit 04863dca1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 0 deletions

View File

@ -30,8 +30,10 @@ start(_Type, _Args) ->
SupRet = emqx_rule_engine_sup:start_link(), SupRet = emqx_rule_engine_sup:start_link(),
ok = emqx_rule_engine:load_rules(), ok = emqx_rule_engine:load_rules(),
emqx_conf:add_handler(emqx_rule_engine:config_key_path(), emqx_rule_engine), emqx_conf:add_handler(emqx_rule_engine:config_key_path(), emqx_rule_engine),
emqx_rule_engine_cli:load(),
SupRet. SupRet.
stop(_State) -> stop(_State) ->
emqx_rule_engine_cli:unload(),
emqx_conf:remove_handler(emqx_rule_engine:config_key_path()), emqx_conf:remove_handler(emqx_rule_engine:config_key_path()),
ok = emqx_rule_events:unload(). ok = emqx_rule_events:unload().

View File

@ -0,0 +1,94 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_rule_engine_cli).
%% API:
-export([load/0, unload/0]).
-export([cmd/1]).
%%================================================================================
%% API funcions
%%================================================================================
load() ->
ok = emqx_ctl:register_command(rules, {?MODULE, cmd}, []).
unload() ->
ok = emqx_ctl:unregister_command(rules).
%%================================================================================
%% Internal exports
%%================================================================================
cmd(["list"]) ->
lists:foreach(
fun pretty_print_rule_summary/1,
emqx_rule_engine:get_rules_ordered_by_ts()
);
cmd(["show", ID]) ->
pretty_print_rule(ID);
cmd(_) ->
emqx_ctl:usage(
[
{"rules list", "List rules"},
{"rules show <RuleID>", "Show a rule"}
]
).
%%================================================================================
%% Internal functions
%%================================================================================
pretty_print_rule_summary(#{id := Id, name := Name, enable := Enable, description := Desc}) ->
emqx_ctl:print("Rule{id=~ts, name=~ts, enabled=~ts, descr=~ts}\n", [
Id, Name, Enable, Desc
]).
%% erlfmt-ignore
pretty_print_rule(ID) ->
case emqx_rule_engine:get_rule(list_to_binary(ID)) of
{ok, #{id := Id, name := Name, description := Descr, enable := Enable,
sql := SQL, created_at := CreatedAt, updated_at := UpdatedAt,
actions := Actions}} ->
emqx_ctl:print(
"Id:\n ~ts\n"
"Name:\n ~ts\n"
"Description:\n ~ts\n"
"Enabled:\n ~ts\n"
"SQL:\n ~ts\n"
"Created at:\n ~ts\n"
"Updated at:\n ~ts\n"
"Actions:\n ~s\n"
,[Id, Name, left_pad(Descr), Enable, left_pad(SQL),
calendar:system_time_to_rfc3339(CreatedAt, [{unit, millisecond}]),
calendar:system_time_to_rfc3339(UpdatedAt, [{unit, millisecond}]),
[left_pad(format_action(A)) || A <- Actions]
]
);
_ ->
ok
end.
%% erlfmt-ignore
format_action(#{func := Func, args := Args}) ->
io_lib:format("Function:\n ~p\n"
"Args:\n ~p\n"
,[Func, maps:without([preprocessed_tmpl], Args)]
).
left_pad(Str) ->
re:replace(Str, "\n", "\n ", [global]).