diff --git a/README.md b/README.md index 42f8598b..49153296 100644 --- a/README.md +++ b/README.md @@ -123,4 +123,8 @@ The default OTA upload should complete in 10 minutes. Use `--deferred` flag to e Print a list of available dashboards and their widgets by using this command: -`$ arduino-cloud-cli dashboard list --show-widgets` \ No newline at end of file +`$ arduino-cloud-cli dashboard list --show-widgets` + +Delete a dashboard with the following command: + +`$ arduino-cloud-cli dashboard delete --id ` diff --git a/cli/dashboard/dashboard.go b/cli/dashboard/dashboard.go index 4d79ac91..4531db1f 100644 --- a/cli/dashboard/dashboard.go +++ b/cli/dashboard/dashboard.go @@ -29,6 +29,7 @@ func NewCommand() *cobra.Command { } dashboardCommand.AddCommand(initListCommand()) + dashboardCommand.AddCommand(initDeleteCommand()) return dashboardCommand } diff --git a/cli/dashboard/delete.go b/cli/dashboard/delete.go new file mode 100644 index 00000000..177177d9 --- /dev/null +++ b/cli/dashboard/delete.go @@ -0,0 +1,57 @@ +// This file is part of arduino-cloud-cli. +// +// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package dashboard + +import ( + "os" + + "github.com/arduino/arduino-cli/cli/errorcodes" + "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/arduino-cloud-cli/command/dashboard" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var deleteFlags struct { + id string +} + +func initDeleteCommand() *cobra.Command { + deleteCommand := &cobra.Command{ + Use: "delete", + Short: "Delete a dashboard", + Long: "Delete a dashboard from Arduino IoT Cloud", + Run: runDeleteCommand, + } + deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Dashboard ID") + deleteCommand.MarkFlagRequired("id") + return deleteCommand +} + +func runDeleteCommand(cmd *cobra.Command, args []string) { + logrus.Infof("Deleting dashboard %s\n", deleteFlags.id) + + params := &dashboard.DeleteParams{ID: deleteFlags.id} + err := dashboard.Delete(params) + if err != nil { + feedback.Errorf("Error during dashboard delete: %v", err) + os.Exit(errorcodes.ErrGeneric) + } + + logrus.Info("Dashboard successfully deleted") +} diff --git a/command/dashboard/delete.go b/command/dashboard/delete.go new file mode 100644 index 00000000..4e62374a --- /dev/null +++ b/command/dashboard/delete.go @@ -0,0 +1,44 @@ +// This file is part of arduino-cloud-cli. +// +// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package dashboard + +import ( + "github.com/arduino/arduino-cloud-cli/internal/config" + "github.com/arduino/arduino-cloud-cli/internal/iot" +) + +// DeleteParams contains the parameters needed to +// delete a dashboard from Arduino IoT Cloud. +type DeleteParams struct { + ID string +} + +// Delete command is used to delete a dashboard +// from Arduino IoT Cloud. +func Delete(params *DeleteParams) error { + conf, err := config.Retrieve() + if err != nil { + return err + } + iotClient, err := iot.NewClient(conf.Client, conf.Secret) + if err != nil { + return err + } + + return iotClient.DashboardDelete(params.ID) +} diff --git a/internal/iot/client.go b/internal/iot/client.go index e63808c5..7d31ad75 100644 --- a/internal/iot/client.go +++ b/internal/iot/client.go @@ -39,6 +39,7 @@ type Client interface { ThingDelete(id string) error ThingShow(id string) (*iotclient.ArduinoThing, error) ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error) + DashboardDelete(id string) error DashboardList() ([]iotclient.ArduinoDashboardv2, error) } @@ -213,6 +214,16 @@ func (cl *client) DashboardList() ([]iotclient.ArduinoDashboardv2, error) { return dashboards, nil } +// DashboardDelete deletes a dashboard from Arduino IoT Cloud. +func (cl *client) DashboardDelete(id string) error { + _, err := cl.api.DashboardsV2Api.DashboardsV2Delete(cl.ctx, id) + if err != nil { + err = fmt.Errorf("deleting dashboard: %w", errorDetail(err)) + return err + } + return nil +} + func (cl *client) setup(client, secret string) error { // Get the access token in exchange of client_id and client_secret tok, err := token(client, secret)