fix: don't crash when command_ctl table not init
This commit is contained in:
parent
b5f24c4f88
commit
b290d2543b
|
@ -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 is_init() of
|
||||||
|
true ->
|
||||||
case ets:match(?CMD_TAB, {{'_', Cmd}, '$1', '_'}) of
|
case ets:match(?CMD_TAB, {{'_', Cmd}, '$1', '_'}) of
|
||||||
[El] -> El;
|
[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,6 +150,8 @@ 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 is_init() of
|
||||||
|
true ->
|
||||||
case ets:tab2list(?CMD_TAB) of
|
case ets:tab2list(?CMD_TAB) of
|
||||||
[] ->
|
[] ->
|
||||||
print("No commands available.~n");
|
print("No commands available.~n");
|
||||||
|
@ -157,6 +164,9 @@ help() ->
|
||||||
end,
|
end,
|
||||||
Cmds
|
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.
|
||||||
|
|
Loading…
Reference in New Issue