Skip to content

Commit

Permalink
Add delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
ziopio committed Oct 12, 2024
1 parent ffbe3cd commit c9f3b91
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rebar3_grisp_io.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ init(State) ->
rebar3_grisp_io_auth,
rebar3_grisp_io_deploy,
rebar3_grisp_io_upload,
rebar3_grisp_io_delete,
rebar3_grisp_io_validate,
rebar3_grisp_io_version
]).
34 changes: 34 additions & 0 deletions src/rebar3_grisp_io_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
% API
-export([auth/3]).
-export([update_package/5]).
-export([delete_package/3]).
-export([deploy_update/4]).
-export([validate_update/3]).

Expand Down Expand Up @@ -87,6 +88,39 @@ update_package(RState, Token, PackageName, PackagePath, Force) ->
error({error, Other})
end.


%% @doc Performs a PUT request tp /grisp-manager/api/update-package/PackageName
%% @param Token is the clear token of the user
%% @param PackageName must have the following format
%% platform.appname.x.y.z.[profilename[+profilename]].tar
-spec delete_package(RState, Token, PackageName) -> Res
when
RState :: rebar_state:t(),
Token :: rebar3_grisp_io_config:clear_token(),
PackageName :: binary(),
Res :: ok | no_return().
delete_package(RState, Token, PackageName) ->
BaseUrl = base_url(RState),
URI = <<"/grisp-manager/api/update-package/", PackageName/binary>>,
Url = <<BaseUrl/binary, URI/binary>>,
Headers = [{<<"authorization">>, bearer_token(Token)}],
Options = insecure_option(RState),
case hackney:request(delete, Url, Headers, Options) of
{ok, 204, _, _} ->
ok;
{ok, 400, _, ClientRef} ->
{ok, _RespBody} = hackney:body(ClientRef),
error(unknown_request);
{ok, 401, _, _} ->
throw(wrong_credentials);
{ok, 403, _, _} ->
throw(forbidden);
{ok, 404, _, _} ->
throw(package_not_found);
Other ->
error({error, Other})
end.

%% @doc Performs a POST request to /grisp-manager/api/deploy-update
%% @param Token is the clear token of the user
%% @param PackageName must have the following format "platform.appname.x.y.z.tar
Expand Down
91 changes: 91 additions & 0 deletions src/rebar3_grisp_io_delete.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
-module(rebar3_grisp_io_delete).

% Callbacks
-export([init/1]).
-export([do/1]).
-export([format_error/1]).

%--- Includes ------------------------------------------------------------------

-include("rebar3_grisp_io.hrl").
-import(rebar3_grisp_io_io, [
abort/1,
abort/2,
ask/2,
console/1,
console/2,
success/1,
success/2]).

%--- API -----------------------------------------------------------------------

-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
Provider = providers:create([
{namespace, ?NAMESPACE},
{name, delete},
{module, ?MODULE},
{bare, true},
{example, "rebar3 grisp-io delete"},
{opts, []},
{profile, [default]},
{short_desc, "Delete an update package"},
{desc, "Delete an update package~n~n" ++
"Example: rebar3 grisp-io delete~n"}
]),
{ok, rebar_state:add_provider(State, Provider)}.

% Questions:
% - How should we target the update group ?

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(RState) ->
{ok, _} = application:ensure_all_started(rebar3_grisp_io),
try
{Args, _} = rebar_state:command_parsed_args(RState),
RelNameArg = proplists:get_value(relname, Args, undefined),
RelVsnArg = proplists:get_value(relvsn, Args, undefined),
{RelName, RelVsn}
= rebar3_grisp_util:select_release(RState, RelNameArg, RelVsnArg),

Config = rebar3_grisp_io_config:read_config(RState),
EncryptedToken = maps:get(encrypted_token, Config),
Password = ask("Local password", password),
Token = rebar3_grisp_io_config:try_decrypt_token(Password,
EncryptedToken),

PackageName = rebar3_grisp_util:update_file_name(RState, RelName,
RelVsn),
rebar3_grisp_io_api:delete_package(RState, Token, PackageName),

success("Package " ++ PackageName ++ " successfully deleted!"),

{ok, RState}
catch
throw:{error, <<"no_process">>} ->
abort("Error: no deployment process is running.");
throw:{error, <<"disconnected">>} ->
abort("Error: the device is not connected to GRiSP.io");
throw:{error, <<"validate_from_unbooted">>} ->
abort("Error: device needs to be rebooted.");
throw:{error, <<"wait_device">>} ->
abort("Error: deployment waiting for device");
throw:{error, <<"download">>} ->
abort("Error: the device is still downloading the updated");
throw:package_not_found ->
abort("Error: package not found");
throw:wrong_local_password ->
abort("Wrong local password. Try again");
throw:wrong_credentials ->
abort("Error: Wrong credentials");
throw:forbidden ->
abort("Error: No permission to perform this operation");
throw:device_does_not_exist->
abort("Error: The given board doesn't exists or isn't linked")
end.

-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).

%--- Internals -----------------------------------------------------------------

0 comments on commit c9f3b91

Please sign in to comment.