From c9f3b91beaa821b96b7501aee75e0b410b917192 Mon Sep 17 00:00:00 2001 From: Luca Succi Date: Sat, 12 Oct 2024 17:25:42 +0200 Subject: [PATCH 1/2] Add delete command --- src/rebar3_grisp_io.erl | 1 + src/rebar3_grisp_io_api.erl | 34 +++++++++++++ src/rebar3_grisp_io_delete.erl | 91 ++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 src/rebar3_grisp_io_delete.erl diff --git a/src/rebar3_grisp_io.erl b/src/rebar3_grisp_io.erl index b191703..1731f06 100644 --- a/src/rebar3_grisp_io.erl +++ b/src/rebar3_grisp_io.erl @@ -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 ]). diff --git a/src/rebar3_grisp_io_api.erl b/src/rebar3_grisp_io_api.erl index 3103c67..38de1b7 100644 --- a/src/rebar3_grisp_io_api.erl +++ b/src/rebar3_grisp_io_api.erl @@ -3,6 +3,7 @@ % API -export([auth/3]). -export([update_package/5]). +-export([delete_package/3]). -export([deploy_update/4]). -export([validate_update/3]). @@ -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 = <>, + 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 diff --git a/src/rebar3_grisp_io_delete.erl b/src/rebar3_grisp_io_delete.erl new file mode 100644 index 0000000..3fad738 --- /dev/null +++ b/src/rebar3_grisp_io_delete.erl @@ -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 ----------------------------------------------------------------- From 0a01b16a12223660f6a427d62668a1431406156e Mon Sep 17 00:00:00 2001 From: Luca Succi Date: Sat, 12 Oct 2024 17:31:03 +0200 Subject: [PATCH 2/2] Fix little error --- src/rebar3_grisp_io_delete.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rebar3_grisp_io_delete.erl b/src/rebar3_grisp_io_delete.erl index 3fad738..116f402 100644 --- a/src/rebar3_grisp_io_delete.erl +++ b/src/rebar3_grisp_io_delete.erl @@ -58,7 +58,8 @@ do(RState) -> RelVsn), rebar3_grisp_io_api:delete_package(RState, Token, PackageName), - success("Package " ++ PackageName ++ " successfully deleted!"), + success(iolist_to_binary(["Package ", PackageName, + " successfully deleted!"])), {ok, RState} catch