From 2c879b7e131f265934e6e1b6f29a9c2eca6a78f2 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Mon, 21 Mar 2022 14:08:49 +0800 Subject: [PATCH] chore: simplify nest case function --- .../src/emqx_mgmt_api_plugins.erl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl index ebf648bfe..1206481e9 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl @@ -271,14 +271,7 @@ upload_install(post, #{body := #{<<"plugin">> := Plugin}}) when is_map(Plugin) - {AppName, _Vsn} = emqx_plugins:parse_name_vsn(FileName), AppDir = filename:join(emqx_plugins:install_dir(), AppName), case filelib:wildcard(AppDir ++ "*.tar.gz") of - [] -> - {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; + [] -> do_install_package(FileName, Bin); OtherVsn -> {400, #{code => '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`">>} }. +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}}) -> {Plugins, _} = emqx_mgmt_api_plugins_proto_v1:describe_package(Name), case format_plugins(Plugins) of