Skip to content

Commit e222bec

Browse files
committed
Added support to multiple id in tagging
1 parent 6e0abbe commit e222bec

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

cli/device/tag/create.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24+
"strings"
2425

2526
"github.com/arduino/arduino-cli/cli/errorcodes"
2627
"github.com/arduino/arduino-cli/cli/feedback"
@@ -32,6 +33,7 @@ import (
3233

3334
type createTagsFlags struct {
3435
id string
36+
ids string
3537
tags map[string]string
3638
}
3739

@@ -49,23 +51,45 @@ func InitCreateTagsCommand() *cobra.Command {
4951
},
5052
}
5153
createTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Device ID")
54+
createTagsCommand.Flags().StringVarP(&flags.ids, "ids", "", "", "Comma-separated list of Device IDs")
5255
createTagsCommand.Flags().StringToStringVar(
5356
&flags.tags,
5457
"tags",
5558
nil,
5659
"Comma-separated list of tags with format <key>=<value>.",
5760
)
58-
createTagsCommand.MarkFlagRequired("id")
5961
createTagsCommand.MarkFlagRequired("tags")
6062
return createTagsCommand
6163
}
6264

6365
func runCreateTagsCommand(flags *createTagsFlags) error {
64-
logrus.Infof("Creating tags on device %s", flags.id)
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)
6589

6690
params := &tag.CreateTagsParams{
67-
ID: flags.id,
68-
Tags: flags.tags,
91+
ID: id,
92+
Tags: tags,
6993
Resource: tag.Device,
7094
}
7195

cli/device/tag/delete.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24+
"strings"
2425

2526
"github.com/arduino/arduino-cli/cli/errorcodes"
2627
"github.com/arduino/arduino-cli/cli/feedback"
@@ -32,6 +33,7 @@ import (
3233

3334
type deleteTagsFlags struct {
3435
id string
36+
ids string
3537
keys []string
3638
}
3739

@@ -49,23 +51,48 @@ func InitDeleteTagsCommand() *cobra.Command {
4951
},
5052
}
5153
deleteTagsCommand.Flags().StringVarP(&flags.id, "id", "i", "", "Device ID")
54+
deleteTagsCommand.Flags().StringVarP(&flags.id, "ids", "", "", "Comma-separated list of Device IDs")
5255
deleteTagsCommand.Flags().StringSliceVarP(&flags.keys, "keys", "k", nil, "Comma-separated list of keys of tags to delete")
53-
deleteTagsCommand.MarkFlagRequired("id")
5456
deleteTagsCommand.MarkFlagRequired("keys")
5557
return deleteTagsCommand
5658
}
5759

5860
func runDeleteTagsCommand(flags *deleteTagsFlags) error {
59-
logrus.Infof("Deleting tags with keys %s", flags.keys)
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)
6087

6188
cred, err := config.RetrieveCredentials()
6289
if err != nil {
6390
return fmt.Errorf("retrieving credentials: %w", err)
6491
}
6592

6693
params := &tag.DeleteTagsParams{
67-
ID: flags.id,
68-
Keys: flags.keys,
94+
ID: id,
95+
Keys: keys,
6996
Resource: tag.Device,
7097
}
7198

0 commit comments

Comments
 (0)