fix(gw-cli): fix start/stop/lookup not works

This commit is contained in:
JianBo He 2021-07-23 14:12:34 +08:00
parent 054623bd56
commit 6f084fe6cf
1 changed files with 17 additions and 9 deletions

View File

@ -31,7 +31,9 @@
-spec load() -> ok. -spec load() -> ok.
load() -> load() ->
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)], Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
lists:foreach(fun(Cmd) -> emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, []) end, Cmds). lists:foreach(fun(Cmd) ->
emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, [])
end, Cmds).
-spec unload() -> ok. -spec unload() -> ok.
unload() -> unload() ->
@ -44,7 +46,6 @@ is_cmd(Fun) ->
_ -> false _ -> false
end. end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Cmds %% Cmds
@ -56,25 +57,25 @@ gateway(["list"]) ->
end, emqx_gateway:list()); end, emqx_gateway:list());
gateway(["lookup", GatewayInstaId]) -> gateway(["lookup", GatewayInstaId]) ->
case emqx_gateway:lookup(GatewayInstaId) of case emqx_gateway:lookup(atom(GatewayInstaId)) of
undefined -> undefined ->
emqx_ctl:print("undefined"); emqx_ctl:print("undefined~n");
Info -> Info ->
emqx_ctl:print("~p~n", [Info]) emqx_ctl:print("~p~n", [Info])
end; end;
gateway(["stop", GatewayInstaId]) -> gateway(["stop", GatewayInstaId]) ->
case emqx_gateway:stop(GatewayInstaId) of case emqx_gateway:stop(atom(GatewayInstaId)) of
ok -> ok ->
emqx_ctl:print("ok"); emqx_ctl:print("ok~n");
{error, Reason} -> {error, Reason} ->
emqx_ctl:print("Error: ~p~n", [Reason]) emqx_ctl:print("Error: ~p~n", [Reason])
end; end;
gateway(["start", GatewayInstaId]) -> gateway(["start", GatewayInstaId]) ->
case emqx_gateway:start(GatewayInstaId) of case emqx_gateway:start(atom(GatewayInstaId)) of
ok -> ok ->
emqx_ctl:print("ok"); emqx_ctl:print("ok~n");
{error, Reason} -> {error, Reason} ->
emqx_ctl:print("Error: ~p~n", [Reason]) emqx_ctl:print("Error: ~p~n", [Reason])
end; end;
@ -136,7 +137,7 @@ gateway(_) ->
Tab = emqx_gateway_metrics:tabname(GatewayType), Tab = emqx_gateway_metrics:tabname(GatewayType),
case ets:info(Tab) of case ets:info(Tab) of
undefined -> undefined ->
emqx_ctl:print("Bad Gateway Tyep.~n"); emqx_ctl:print("Bad Gateway Type.~n");
_ -> _ ->
lists:foreach( lists:foreach(
fun({K, V}) -> fun({K, V}) ->
@ -149,6 +150,13 @@ gateway(_) ->
"List all metrics for a type of gateway"} "List all metrics for a type of gateway"}
]). ]).
atom(Id) ->
try
list_to_existing_atom(Id)
catch
_ : _ -> undefined
end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal funcs %% Internal funcs
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------