fix: don't crash when command_ctl table not init

This commit is contained in:
Zhongwen Deng 2023-05-23 16:40:55 +08:00
parent b5f24c4f88
commit b290d2543b
1 changed files with 30 additions and 17 deletions

View File

@ -128,16 +128,21 @@ run_command(Cmd, Args) when is_atom(Cmd) ->
}), }),
{error, Reason} {error, Reason}
end; end;
[] -> Error ->
help(), help(),
{error, cmd_not_found} Error
end. end.
-spec lookup_command(cmd()) -> [{module(), atom()}]. -spec lookup_command(cmd()) -> [{module(), atom()}].
lookup_command(Cmd) when is_atom(Cmd) -> lookup_command(Cmd) when is_atom(Cmd) ->
case ets:match(?CMD_TAB, {{'_', Cmd}, '$1', '_'}) of case is_init() of
[El] -> El; true ->
[] -> [] case ets:match(?CMD_TAB, {{'_', Cmd}, '$1', '_'}) of
[El] -> El;
[] -> {error, cmd_not_found}
end;
false ->
{error, cmd_is_initializing}
end. end.
-spec get_commands() -> list({cmd(), module(), atom()}). -spec get_commands() -> list({cmd(), module(), atom()}).
@ -145,18 +150,23 @@ get_commands() ->
[{Cmd, M, F} || {{_Seq, Cmd}, {M, F}, _Opts} <- ets:tab2list(?CMD_TAB)]. [{Cmd, M, F} || {{_Seq, Cmd}, {M, F}, _Opts} <- ets:tab2list(?CMD_TAB)].
help() -> help() ->
case ets:tab2list(?CMD_TAB) of case is_init() of
[] -> true ->
print("No commands available.~n"); case ets:tab2list(?CMD_TAB) of
Cmds -> [] ->
print("Usage: ~ts~n", ["emqx ctl"]), print("No commands available.~n");
lists:foreach( Cmds ->
fun({_, {Mod, Cmd}, _}) -> print("Usage: ~ts~n", ["emqx ctl"]),
print("~110..-s~n", [""]), lists:foreach(
apply(Mod, Cmd, [usage]) fun({_, {Mod, Cmd}, _}) ->
end, print("~110..-s~n", [""]),
Cmds apply(Mod, Cmd, [usage])
) end,
Cmds
)
end;
false ->
print("Command table is initializing.~n")
end. end.
-spec print(io:format()) -> ok. -spec print(io:format()) -> ok.
@ -279,3 +289,6 @@ safe_to_existing_atom(Str) ->
_:badarg -> _:badarg ->
undefined undefined
end. end.
is_init() ->
ets:info(?CMD_TAB) =/= undefined.