-
Notifications
You must be signed in to change notification settings - Fork 5
/
handle_args.go
100 lines (85 loc) · 2.18 KB
/
handle_args.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
package main
import (
"errors"
"fmt"
"os"
"time"
"github.com/apex/log"
"github.com/fatih/color"
"github.com/jckuester/awsrm/pkg/resource"
"github.com/jckuester/awstools-lib/aws"
"github.com/jckuester/awstools-lib/terraform"
"golang.org/x/net/context"
)
func handleInputFromArgs(ctx context.Context, args []string, profile, region string, force bool, dryRun bool) int {
log.Debug("input via args")
rType := resource.PrefixResourceType(args[0])
if !terraform.IsType(rType) {
fmt.Fprint(os.Stderr, color.RedString("\nError: no resource type found: %s\n", rType))
return 1
}
var profiles []string
var regions []string
if profile != "" {
profiles = []string{profile}
} else {
env, ok := os.LookupEnv("AWS_PROFILE")
if ok {
profiles = []string{env}
}
}
if region != "" {
regions = []string{region}
}
clients, err := aws.NewClientPool(ctx, profiles, regions)
if err != nil {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))
return 1
}
var resources []terraform.Resource
for _, client := range clients {
for _, id := range args[1:] {
resources = append(resources, terraform.Resource{
Type: rType,
ID: id,
Profile: client.Profile,
Region: client.Region,
})
}
}
var clientKeys []aws.ClientKey
for k := range clients {
clientKeys = append(clientKeys, k)
}
providers, err := terraform.NewProviderPool(ctx, clientKeys, terraformAwsProviderVersion, "~/.awsrm", 1*time.Minute)
if err != nil {
if !errors.Is(err, context.Canceled) {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))
}
return 1
}
defer func() {
for _, p := range providers {
_ = p.Close()
}
}()
resourcesCh := make(chan resource.UpdatedResources, 1)
go func() { resourcesCh <- resource.Update(resources, providers) }()
select {
case <-ctx.Done():
return 1
case result := <-resourcesCh:
resources = result.Resources
for _, err := range result.Errors {
fmt.Fprint(os.Stderr, color.RedString("Error %s: %s\n", rType, err))
}
}
doneDelete := make(chan bool, 1)
go func() { resource.Delete(resources, os.Stdin, force, dryRun, doneDelete) }()
select {
case <-ctx.Done():
return 0
case <-doneDelete:
}
return 0
}