/
untag.go
55 lines (44 loc) · 1.23 KB
/
untag.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
package cmd
import (
"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"
)
// untagCmd represents the untag command
var untagCmd = &cobra.Command{
Use: "untag",
Short: "Remove instance tags",
RunE: doUntag,
}
var untagOpts = struct {
dryRun bool
instanceIDs []string
tagStrings []string
}{}
func doUntag(cmd *cobra.Command, args []string) error {
svc := ec2.New(session.New(), &aws.Config{})
tags := []*ec2.Tag{}
for _, tagString := range untagOpts.tagStrings {
tags = append(tags, &ec2.Tag{
Key: aws.String(tagString),
})
}
opts := &ec2.DeleteTagsInput{
DryRun: aws.Bool(untagOpts.dryRun),
Resources: aws.StringSlice(untagOpts.instanceIDs),
Tags: tags,
}
_, err := svc.DeleteTags(opts)
if err != nil {
return errors.Wrap(err, "failed to execute DeleteTags")
}
return nil
}
func init() {
RootCmd.AddCommand(untagCmd)
untagCmd.Flags().BoolVar(&untagOpts.dryRun, "dry-run", false, "Dry run")
untagCmd.Flags().StringSliceVar(&untagOpts.instanceIDs, "instances", []string{}, "Instance IDs")
untagCmd.Flags().StringSliceVar(&untagOpts.tagStrings, "tags", []string{}, "KEY=value tags")
}