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}
end;
[] ->
Error ->
help(),
{error, cmd_not_found}
Error
end.
-spec lookup_command(cmd()) -> [{module(), atom()}].
lookup_command(Cmd) when is_atom(Cmd) ->
case is_init() of
true ->
case ets:match(?CMD_TAB, {{'_', Cmd}, '$1', '_'}) of
[El] -> El;
[] -> []
[] -> {error, cmd_not_found}
end;
false ->
{error, cmd_is_initializing}
end.
-spec get_commands() -> list({cmd(), module(), atom()}).
@ -145,6 +150,8 @@ get_commands() ->
[{Cmd, M, F} || {{_Seq, Cmd}, {M, F}, _Opts} <- ets:tab2list(?CMD_TAB)].
help() ->
case is_init() of
true ->
case ets:tab2list(?CMD_TAB) of
[] ->
print("No commands available.~n");
@ -157,6 +164,9 @@ help() ->
end,
Cmds
)
end;
false ->
print("Command table is initializing.~n")
end.
-spec print(io:format()) -> ok.
@ -279,3 +289,6 @@ safe_to_existing_atom(Str) ->
_:badarg ->
undefined
end.
is_init() ->
ets:info(?CMD_TAB) =/= undefined.