Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Delete things by tags or id
  • Loading branch information
polldo committed Nov 8, 2021
commit 31d1c3597908195eb15bc519fba9f2f7d0fc8f0c
17 changes: 14 additions & 3 deletions cli/thing/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
)

var deleteFlags struct {
id string
id string
tags map[string]string
}

func initDeleteCommand() *cobra.Command {
Expand All @@ -39,14 +40,24 @@ func initDeleteCommand() *cobra.Command {
Run: runDeleteCommand,
}
deleteCommand.Flags().StringVarP(&deleteFlags.id, "id", "i", "", "Thing ID")
deleteCommand.MarkFlagRequired("id")
// delete only the things that have all the passed tags
deleteCommand.Flags().StringToStringVar(
&deleteFlags.tags,
"tags",
nil,
"List of comma-separated tags. A tag has this format: <key>=<value>",
)
return deleteCommand
}

func runDeleteCommand(cmd *cobra.Command, args []string) {
logrus.Infof("Deleting thing %s\n", deleteFlags.id)

params := &thing.DeleteParams{ID: deleteFlags.id}
params := &thing.DeleteParams{Tags: deleteFlags.tags}
if deleteFlags.id != "" {
params.ID = &deleteFlags.id
}

err := thing.Delete(params)
if err != nil {
feedback.Errorf("Error during thing delete: %v", err)
Expand Down
37 changes: 35 additions & 2 deletions command/thing/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@
package thing

import (
"errors"

"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)

// DeleteParams contains the parameters needed to
// delete a thing from Arduino IoT Cloud.
// ID and Tags parameters are mutually exclusive
// and one among them is required: An error is returned
// if they are both nil or if they are both not nil.
type DeleteParams struct {
ID string
ID *string // Should be nil if Tags is not nil
Tags map[string]string // Should be nil if ID is not nil
}

// Delete command is used to delete a thing
// from Arduino IoT Cloud.
func Delete(params *DeleteParams) error {
if params.ID == nil && params.Tags == nil {
return errors.New("provide either ID or Tags")
} else if params.ID != nil && params.Tags != nil {
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
}

conf, err := config.Retrieve()
if err != nil {
return err
Expand All @@ -40,5 +52,26 @@ func Delete(params *DeleteParams) error {
return err
}

return iotClient.ThingDelete(params.ID)
if params.ID != nil {
// Delete by ID
return iotClient.ThingDelete(*params.ID)

} else if params.Tags != nil {
things, err := iotClient.ThingList(nil, nil, false, params.Tags)
if err != nil {
return err
}
for _, t := range things {
err = iotClient.ThingDelete(t.Id)
if err != nil {
return err
}
}

} else {
// should not be reachable
return errors.New("provide either '--id' or '--tags' flag")
}

return nil
}