github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/thing/tag/create.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 createTagsFlags struct {
    34  	id   string
    35  	tags map[string]string
    36  }
    37  
    38  func InitCreateTagsCommand() *cobra.Command {
    39  	flags := &createTagsFlags{}
    40  	createTagsCommand := &cobra.Command{
    41  		Use:   "create-tags",
    42  		Short: "Create or overwrite tags on a thing",
    43  		Long:  "Create or overwrite tags on a thing of Arduino IoT Cloud",
    44  		Run: func(cmd *cobra.Command, args []string) {
    45  			if err := runCreateTagsCommand(flags); err != nil {
    46  				feedback.Errorf("Error during thing create-tags: %v", err)
    47  				os.Exit(errorcodes.ErrGeneric)
    48  			}
    49  		},
    50  	}
    51  	createTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Thing ID")
    52  	createTagsCommand.Flags().StringToStringVar(
    53  		&flags.tags,
    54  		"tags",
    55  		nil,
    56  		"Comma-separated list of tags with format <key>=<value>.",
    57  	)
    58  	createTagsCommand.MarkFlagRequired("id")
    59  	createTagsCommand.MarkFlagRequired("tags")
    60  	return createTagsCommand
    61  }
    62  
    63  func runCreateTagsCommand(flags *createTagsFlags) error {
    64  	logrus.Infof("Creating tags on thing %s", flags.id)
    65  
    66  	params := &tag.CreateTagsParams{
    67  		ID:       flags.id,
    68  		Tags:     flags.tags,
    69  		Resource: tag.Thing,
    70  	}
    71  
    72  	cred, err := config.RetrieveCredentials()
    73  	if err != nil {
    74  		return fmt.Errorf("retrieving credentials: %w", err)
    75  	}
    76  
    77  	err = tag.CreateTags(context.TODO(), params, cred)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	logrus.Info("Tags successfully created")
    83  	return nil
    84  }