-
Notifications
You must be signed in to change notification settings - Fork 7
/
tag.go
67 lines (54 loc) · 1.46 KB
/
tag.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
package cmd
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// tagCmd represents the tag command
var tagCmd = &cobra.Command{
Use: "tag",
Short: "Add tags to EC2 instance",
RunE: doTag,
}
var tagOpts = struct {
dryRun bool
instanceIDs []string
tagStrings []string
}{}
func doTag(cmd *cobra.Command, args []string) error {
svc := ec2.New(session.New(), &aws.Config{})
tags := []*ec2.Tag{}
for _, tagString := range tagOpts.tagStrings {
keyValue := strings.Split(tagString, "=")
if len(keyValue) == 1 {
tags = append(tags, &ec2.Tag{
Key: aws.String(keyValue[0]),
Value: aws.String(""),
})
} else {
tags = append(tags, &ec2.Tag{
Key: aws.String(keyValue[0]),
Value: aws.String(strings.Join(keyValue[1:], "=")),
})
}
}
opts := &ec2.CreateTagsInput{
DryRun: aws.Bool(tagOpts.dryRun),
Resources: aws.StringSlice(tagOpts.instanceIDs),
Tags: tags,
}
_, err := svc.CreateTags(opts)
if err != nil {
return errors.Wrap(err, "failed to execute CreateTags")
}
return nil
}
func init() {
RootCmd.AddCommand(tagCmd)
tagCmd.Flags().BoolVar(&tagOpts.dryRun, "dry-run", false, "Dry run")
tagCmd.Flags().StringSliceVar(&tagOpts.instanceIDs, "instances", []string{}, "Instance IDs")
tagCmd.Flags().StringSliceVar(&tagOpts.tagStrings, "tags", []string{}, "KEY=value tags")
}