github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/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  	"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 createTagsFlags struct {
    35  	id   string
    36  	ids  string
    37  	tags map[string]string
    38  }
    39  
    40  func InitCreateTagsCommand() *cobra.Command {
    41  	flags := &createTagsFlags{}
    42  	createTagsCommand := &cobra.Command{
    43  		Use:   "create-tags",
    44  		Short: "Create or overwrite tags on a device",
    45  		Long:  "Create or overwrite tags on a device of Arduino IoT Cloud",
    46  		Run: func(cmd *cobra.Command, args []string) {
    47  			if err := runCreateTagsCommand(flags); err != nil {
    48  				feedback.Errorf("Error during device create-tags: %v", err)
    49  				os.Exit(errorcodes.ErrGeneric)
    50  			}
    51  		},
    52  	}
    53  	createTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Device ID")
    54  	createTagsCommand.Flags().StringVarP(&flags.ids, "ids", "", "", "Comma-separated list of Device IDs")
    55  	createTagsCommand.Flags().StringToStringVar(
    56  		&flags.tags,
    57  		"tags",
    58  		nil,
    59  		"Comma-separated list of tags with format <key>=<value>.",
    60  	)
    61  	createTagsCommand.MarkFlagRequired("tags")
    62  	return createTagsCommand
    63  }
    64  
    65  func runCreateTagsCommand(flags *createTagsFlags) error {
    66  	if flags.id == "" && flags.ids == "" {
    67  		return fmt.Errorf("missing required flag(s) \"id\" or \"ids\"")
    68  	}
    69  
    70  	if flags.id != "" {
    71  		if err := creteTag(flags.id, flags.tags); err != nil {
    72  			return err
    73  		}
    74  	}
    75  	if flags.ids != "" {
    76  		idsArray := strings.Split(flags.ids, ",")
    77  		for _, id := range idsArray {
    78  			id = strings.TrimSpace(id)
    79  			if err := creteTag(id, flags.tags); err != nil {
    80  				return err
    81  			}
    82  		}
    83  	}
    84  	return nil
    85  }
    86  
    87  func creteTag(id string, tags map[string]string) error {
    88  	logrus.Infof("Creating tags on device %s", id)
    89  
    90  	params := &tag.CreateTagsParams{
    91  		ID:       id,
    92  		Tags:     tags,
    93  		Resource: tag.Device,
    94  	}
    95  
    96  	cred, err := config.RetrieveCredentials()
    97  	if err != nil {
    98  		return fmt.Errorf("retrieving credentials: %w", err)
    99  	}
   100  
   101  	if err = tag.CreateTags(context.TODO(), params, cred); err != nil {
   102  		return err
   103  	}
   104  
   105  	logrus.Info("Tags successfully created")
   106  	return nil
   107  }