Skip to content

Commit 382b7c2

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Add dashboard delete command (#46)
Add command to delete dashboards: arduino-cloud-cli dashboard delete --id <dashboardID> * Add dashboard delete command * Update readme
1 parent e355ad9 commit 382b7c2

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,8 @@ The default OTA upload should complete in 10 minutes. Use `--deferred` flag to e
123123

124124
Print a list of available dashboards and their widgets by using this command:
125125

126-
`$ arduino-cloud-cli dashboard list --show-widgets`
126+
`$ arduino-cloud-cli dashboard list --show-widgets`
127+
128+
Delete a dashboard with the following command:
129+
130+
`$ arduino-cloud-cli dashboard delete --id <dashboardID>`

cli/dashboard/dashboard.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func NewCommand() *cobra.Command {
2929
}
3030

3131
dashboardCommand.AddCommand(initListCommand())
32+
dashboardCommand.AddCommand(initDeleteCommand())
3233

3334
return dashboardCommand
3435
}

cli/dashboard/delete.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package dashboard
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/dashboard"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var deleteFlags struct {
31+
id string
32+
}
33+
34+
func initDeleteCommand() *cobra.Command {
35+
deleteCommand := &cobra.Command{
36+
Use: "delete",
37+
Short: "Delete a dashboard",
38+
Long: "Delete a dashboard from Arduino IoT Cloud",
39+
Run: runDeleteCommand,
40+
}
41+
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Dashboard ID")
42+
deleteCommand.MarkFlagRequired("id")
43+
return deleteCommand
44+
}
45+
46+
func runDeleteCommand(cmd *cobra.Command, args []string) {
47+
logrus.Infof("Deleting dashboard %s\n", deleteFlags.id)
48+
49+
params := &dashboard.DeleteParams{ID: deleteFlags.id}
50+
err := dashboard.Delete(params)
51+
if err != nil {
52+
feedback.Errorf("Error during dashboard delete: %v", err)
53+
os.Exit(errorcodes.ErrGeneric)
54+
}
55+
56+
logrus.Info("Dashboard successfully deleted")
57+
}

command/dashboard/delete.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package dashboard
19+
20+
import (
21+
"github.com/arduino/arduino-cloud-cli/internal/config"
22+
"github.com/arduino/arduino-cloud-cli/internal/iot"
23+
)
24+
25+
// DeleteParams contains the parameters needed to
26+
// delete a dashboard from Arduino IoT Cloud.
27+
type DeleteParams struct {
28+
ID string
29+
}
30+
31+
// Delete command is used to delete a dashboard
32+
// from Arduino IoT Cloud.
33+
func Delete(params *DeleteParams) error {
34+
conf, err := config.Retrieve()
35+
if err != nil {
36+
return err
37+
}
38+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
39+
if err != nil {
40+
return err
41+
}
42+
43+
return iotClient.DashboardDelete(params.ID)
44+
}

internal/iot/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Client interface {
3939
ThingDelete(id string) error
4040
ThingShow(id string) (*iotclient.ArduinoThing, error)
4141
ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
42+
DashboardDelete(id string) error
4243
DashboardList() ([]iotclient.ArduinoDashboardv2, error)
4344
}
4445

@@ -213,6 +214,16 @@ func (cl *client) DashboardList() ([]iotclient.ArduinoDashboardv2, error) {
213214
return dashboards, nil
214215
}
215216

217+
// DashboardDelete deletes a dashboard from Arduino IoT Cloud.
218+
func (cl *client) DashboardDelete(id string) error {
219+
_, err := cl.api.DashboardsV2Api.DashboardsV2Delete(cl.ctx, id)
220+
if err != nil {
221+
err = fmt.Errorf("deleting dashboard: %w", errorDetail(err))
222+
return err
223+
}
224+
return nil
225+
}
226+
216227
func (cl *client) setup(client, secret string) error {
217228
// Get the access token in exchange of client_id and client_secret
218229
tok, err := token(client, secret)

0 commit comments

Comments
 (0)