chore: simplify nest case function

This commit is contained in:
Zhongwen Deng 2022-03-21 14:08:49 +08:00
parent bf6339ebf4
commit 2c879b7e13
1 changed files with 10 additions and 8 deletions

View File

@ -271,14 +271,7 @@ upload_install(post, #{body := #{<<"plugin">> := Plugin}}) when is_map(Plugin) -
{AppName, _Vsn} = emqx_plugins:parse_name_vsn(FileName), {AppName, _Vsn} = emqx_plugins:parse_name_vsn(FileName),
AppDir = filename:join(emqx_plugins:install_dir(), AppName), AppDir = filename:join(emqx_plugins:install_dir(), AppName),
case filelib:wildcard(AppDir ++ "*.tar.gz") of case filelib:wildcard(AppDir ++ "*.tar.gz") of
[] -> [] -> do_install_package(FileName, Bin);
{Res, _} = emqx_mgmt_api_plugins_proto_v1:install_package(FileName, Bin),
case lists:filter(fun(R) -> R =/= ok end, Res) of
[] -> {200};
[{error, Reason} | _] ->
{400, #{code => 'UNEXPECTED_ERROR',
message => iolist_to_binary(io_lib:format("~p", [Reason]))}}
end;
OtherVsn -> OtherVsn ->
{400, #{code => 'ALREADY_INSTALLED', {400, #{code => 'ALREADY_INSTALLED',
message => iolist_to_binary(io_lib:format("~p already installed", message => iolist_to_binary(io_lib:format("~p already installed",
@ -294,6 +287,15 @@ upload_install(post, #{}) ->
<<"form-data should be `plugin=@packagename-vsn.tar.gz;type=application/x-gzip`">>} <<"form-data should be `plugin=@packagename-vsn.tar.gz;type=application/x-gzip`">>}
}. }.
do_install_package(FileName, Bin) ->
{Res, _} = emqx_mgmt_api_plugins_proto_v1:install_package(FileName, Bin),
case lists:filter(fun(R) -> R =/= ok end, Res) of
[] -> {200};
[{error, Reason} | _] ->
{400, #{code => 'UNEXPECTED_ERROR',
message => iolist_to_binary(io_lib:format("~p", [Reason]))}}
end.
plugin(get, #{bindings := #{name := Name}}) -> plugin(get, #{bindings := #{name := Name}}) ->
{Plugins, _} = emqx_mgmt_api_plugins_proto_v1:describe_package(Name), {Plugins, _} = emqx_mgmt_api_plugins_proto_v1:describe_package(Name),
case format_plugins(Plugins) of case format_plugins(Plugins) of