feat: add --json option to 'emqx ctl cluster status' command

This commit is contained in:
Zaiming (Stone) Shi 2022-06-23 21:59:11 +02:00
parent 2cdf95aaa3
commit 18f3920a73
1 changed files with 22 additions and 1 deletions

View File

@ -135,14 +135,35 @@ cluster(["force-leave", SNode]) ->
end;
cluster(["status"]) ->
emqx_ctl:print("Cluster status: ~p~n", [ekka_cluster:info()]);
cluster(["status", "--json"]) ->
Info = sort_map_list_fields(ekka_cluster:info()),
emqx_ctl:print("~ts~n", [emqx_logger_jsonfmt:best_effort_json(Info)]);
cluster(_) ->
emqx_ctl:usage([
{"cluster join <Node>", "Join the cluster"},
{"cluster leave", "Leave the cluster"},
{"cluster force-leave <Node>", "Force the node leave from cluster"},
{"cluster status", "Cluster status"}
{"cluster status [--json]", "Cluster status"}
]).
%% sort lists for deterministic output
sort_map_list_fields(Map) when is_map(Map) ->
lists:foldl(
fun(Field, Acc) ->
sort_map_list_field(Field, Acc)
end,
Map,
maps:keys(Map)
);
sort_map_list_fields(NotMap) ->
NotMap.
sort_map_list_field(Field, Map) ->
case maps:get(Field, Map) of
[_ | _] = L -> Map#{Field := lists:sort(L)};
_ -> Map
end.
%%--------------------------------------------------------------------
%% @doc Query clients