github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/thing/delete.go (about) 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 thing 19 20 import ( 21 "context" 22 "fmt" 23 "os" 24 25 "github.com/arduino/arduino-cli/cli/errorcodes" 26 "github.com/arduino/arduino-cli/cli/feedback" 27 "github.com/arduino/arduino-cloud-cli/command/thing" 28 "github.com/arduino/arduino-cloud-cli/config" 29 "github.com/sirupsen/logrus" 30 "github.com/spf13/cobra" 31 ) 32 33 type deleteFlags struct { 34 id string 35 tags map[string]string 36 } 37 38 func initDeleteCommand() *cobra.Command { 39 flags := &deleteFlags{} 40 deleteCommand := &cobra.Command{ 41 Use: "delete", 42 Short: "Delete a thing", 43 Long: "Delete a thing from Arduino IoT Cloud", 44 Run: func(cmd *cobra.Command, args []string) { 45 if err := runDeleteCommand(flags); err != nil { 46 feedback.Errorf("Error during thing delete: %v", err) 47 os.Exit(errorcodes.ErrGeneric) 48 } 49 }, 50 } 51 deleteCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Thing ID") 52 deleteCommand.Flags().StringToStringVar( 53 &flags.tags, 54 "tags", 55 nil, 56 "Comma-separated list of tags with format <key>=<value>.\n"+ 57 "Delete all things that match the provided tags.\n"+ 58 "Mutually exclusive with '--id'.", 59 ) 60 return deleteCommand 61 } 62 63 func runDeleteCommand(flags *deleteFlags) error { 64 logrus.Infof("Deleting thing %s", flags.id) 65 66 cred, err := config.RetrieveCredentials() 67 if err != nil { 68 return fmt.Errorf("retrieving credentials: %w", err) 69 } 70 71 params := &thing.DeleteParams{Tags: flags.tags} 72 if flags.id != "" { 73 params.ID = &flags.id 74 } 75 76 err = thing.Delete(context.TODO(), params, cred) 77 if err != nil { 78 return err 79 } 80 81 logrus.Info("Thing successfully deleted") 82 return nil 83 }