-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate.go
107 lines (95 loc) · 2.92 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tag
import (
"context"
"fmt"
"os"
"strings"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/command/tag"
"github.com/arduino/arduino-cloud-cli/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type createTagsFlags struct {
id string
ids string
tags map[string]string
}
func InitCreateTagsCommand() *cobra.Command {
flags := &createTagsFlags{}
createTagsCommand := &cobra.Command{
Use: "create-tags",
Short: "Create or overwrite tags on a device",
Long: "Create or overwrite tags on a device of Arduino IoT Cloud",
Run: func(cmd *cobra.Command, args []string) {
if err := runCreateTagsCommand(flags); err != nil {
feedback.Errorf("Error during device create-tags: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
},
}
createTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Device ID")
createTagsCommand.Flags().StringVarP(&flags.ids, "ids", "", "", "Comma-separated list of Device IDs")
createTagsCommand.Flags().StringToStringVar(
&flags.tags,
"tags",
nil,
"Comma-separated list of tags with format <key>=<value>.",
)
createTagsCommand.MarkFlagRequired("tags")
return createTagsCommand
}
func runCreateTagsCommand(flags *createTagsFlags) error {
if flags.id == "" && flags.ids == "" {
return fmt.Errorf("missing required flag(s) \"id\" or \"ids\"")
}
if flags.id != "" {
if err := creteTag(flags.id, flags.tags); err != nil {
return err
}
}
if flags.ids != "" {
idsArray := strings.Split(flags.ids, ",")
for _, id := range idsArray {
id = strings.TrimSpace(id)
if err := creteTag(id, flags.tags); err != nil {
return err
}
}
}
return nil
}
func creteTag(id string, tags map[string]string) error {
logrus.Infof("Creating tags on device %s", id)
params := &tag.CreateTagsParams{
ID: id,
Tags: tags,
Resource: tag.Device,
}
cred, err := config.RetrieveCredentials()
if err != nil {
return fmt.Errorf("retrieving credentials: %w", err)
}
if err = tag.CreateTags(context.TODO(), params, cred); err != nil {
return err
}
logrus.Info("Tags successfully created")
return nil
}