github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/thing/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  
    25  	"github.com/arduino/arduino-cli/cli/errorcodes"
    26  	"github.com/arduino/arduino-cli/cli/feedback"
    27  	"github.com/arduino/arduino-cloud-cli/command/tag"
    28  	"github.com/arduino/arduino-cloud-cli/config"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  type deleteTagsFlags struct {
    34  	id   string
    35  	keys []string
    36  }
    37  
    38  func InitDeleteTagsCommand() *cobra.Command {
    39  	flags := &deleteTagsFlags{}
    40  	deleteTagsCommand := &cobra.Command{
    41  		Use:   "delete-tags",
    42  		Short: "Delete tags of a thing",
    43  		Long:  "Delete tags of a thing of Arduino IoT Cloud",
    44  		Run: func(cmd *cobra.Command, args []string) {
    45  			if err := runDeleteTagsCommand(flags); err != nil {
    46  				feedback.Errorf("Error during thing delete-tags: %v", err)
    47  				os.Exit(errorcodes.ErrGeneric)
    48  			}
    49  		},
    50  	}
    51  	deleteTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Thing ID")
    52  	deleteTagsCommand.Flags().StringSliceVarP(&flags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete")
    53  	deleteTagsCommand.MarkFlagRequired("id")
    54  	deleteTagsCommand.MarkFlagRequired("keys")
    55  	return deleteTagsCommand
    56  }
    57  
    58  func runDeleteTagsCommand(flags *deleteTagsFlags) error {
    59  	logrus.Infof("Deleting tags with keys %s", flags.keys)
    60  
    61  	cred, err := config.RetrieveCredentials()
    62  	if err != nil {
    63  		return fmt.Errorf("retrieving credentials: %w", err)
    64  	}
    65  
    66  	params := &tag.DeleteTagsParams{
    67  		ID:       flags.id,
    68  		Keys:     flags.keys,
    69  		Resource: tag.Thing,
    70  	}
    71  
    72  	err = tag.DeleteTags(context.TODO(), params, cred)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	logrus.Info("Tags successfully deleted")
    78  	return nil
    79  }