github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/tag/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 tag 19 20 import ( 21 "context" 22 "fmt" 23 "os" 24 "strings" 25 26 "github.com/arduino/arduino-cli/cli/errorcodes" 27 "github.com/arduino/arduino-cli/cli/feedback" 28 "github.com/arduino/arduino-cloud-cli/command/tag" 29 "github.com/arduino/arduino-cloud-cli/config" 30 "github.com/sirupsen/logrus" 31 "github.com/spf13/cobra" 32 ) 33 34 type deleteTagsFlags struct { 35 id string 36 ids string 37 keys []string 38 } 39 40 func InitDeleteTagsCommand() *cobra.Command { 41 flags := &deleteTagsFlags{} 42 deleteTagsCommand := &cobra.Command{ 43 Use: "delete-tags", 44 Short: "Delete tags of a device", 45 Long: "Delete tags of a device of Arduino IoT Cloud", 46 Run: func(cmd *cobra.Command, args []string) { 47 if err := runDeleteTagsCommand(flags); err != nil { 48 feedback.Errorf("Error during device delete-tags: %v", err) 49 os.Exit(errorcodes.ErrGeneric) 50 } 51 }, 52 } 53 deleteTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Device ID") 54 deleteTagsCommand.Flags().StringVarP(&flags.id, "ids", "", "", "Comma-separated list of Device IDs") 55 deleteTagsCommand.Flags().StringSliceVarP(&flags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete") 56 deleteTagsCommand.MarkFlagRequired("keys") 57 return deleteTagsCommand 58 } 59 60 func runDeleteTagsCommand(flags *deleteTagsFlags) error { 61 if flags.id == "" && flags.ids == "" { 62 return fmt.Errorf("missing required flag(s) \"id\" or \"ids\"") 63 } 64 65 if flags.id != "" { 66 err := deleteTags(flags.id, flags.keys) 67 if err != nil { 68 return err 69 } 70 } 71 if flags.ids != "" { 72 ids := strings.Split(flags.ids, ",") 73 for _, id := range ids { 74 id = strings.TrimSpace(id) 75 err := deleteTags(id, flags.keys) 76 if err != nil { 77 return err 78 } 79 } 80 } 81 82 return nil 83 } 84 85 func deleteTags(id string, keys []string) error { 86 logrus.Infof("Deleting tags with keys %s", keys) 87 88 cred, err := config.RetrieveCredentials() 89 if err != nil { 90 return fmt.Errorf("retrieving credentials: %w", err) 91 } 92 93 params := &tag.DeleteTagsParams{ 94 ID: id, 95 Keys: keys, 96 Resource: tag.Device, 97 } 98 99 err = tag.DeleteTags(context.TODO(), params, cred) 100 if err != nil { 101 return err 102 } 103 104 logrus.Info("Tags successfully deleted") 105 return nil 106 }