-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
123 lines (96 loc) · 2.92 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"io/ioutil"
stdlog "log"
"os"
"os/signal"
"strings"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/fatih/color"
"github.com/jckuester/awsrm/internal"
flag "github.com/spf13/pflag"
)
const terraformAwsProviderVersion = "v3.42.0"
func main() {
os.Exit(mainExitCode())
}
func mainExitCode() int {
var logDebug bool
var version bool
var profile string
var region string
var force bool
var dryRun bool
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flags.Usage = func() {
printHelp(flags)
}
flags.BoolVar(&logDebug, "debug", false, "Enable debug logging")
flags.BoolVar(&force, "force", false, "Delete without asking for confirmation. Use with caution!")
flags.BoolVar(&dryRun, "dry-run", false, "Don't delete anything, just show what would be deleted")
flags.StringVarP(&profile, "profile", "p", "", "The AWS profile for the account to delete resources in")
flags.StringVarP(®ion, "region", "r", "", "The region to delete resources in")
flags.BoolVar(&version, "version", false, "Show application version")
_ = flags.Parse(os.Args[1:])
args := flags.Args()
fmt.Println()
defer fmt.Println()
// discard TRACE logs of GRPCProvider
stdlog.SetOutput(ioutil.Discard)
log.SetHandler(cli.Default)
if logDebug {
log.SetLevel(log.DebugLevel)
}
if version {
fmt.Println(internal.BuildVersionString())
return 0
}
ctx := context.Background()
// trap Ctrl+C and call cancel on the context
// to close running Terraform AWS provider plugins properly
ctx, cancel := context.WithCancel(ctx)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, ignoreSignals...)
signal.Notify(signalCh, forwardSignals...)
defer func() {
signal.Stop(signalCh)
cancel()
}()
go func() {
select {
case <-signalCh:
fmt.Fprint(os.Stderr, color.RedString("\nAborting...\n"))
cancel()
case <-ctx.Done():
}
}()
if isInputFromPipe() {
return handleInputFromPipe(ctx, force, dryRun)
}
if len(args) < 2 {
printHelp(flags)
return 1
}
return handleInputFromArgs(ctx, args, profile, region, force, dryRun)
}
func printHelp(fs *flag.FlagSet) {
fmt.Fprintf(os.Stderr, "\n"+strings.TrimSpace(help)+"\n")
fs.PrintDefaults()
}
const help = `
awsrm - A remove command for AWS resources.
USAGE:
$ awsrm [flags] <resource_type> <id> [<id>...]
The resource type and ID(s) are required arguments to delete resource(s).
If no profile and/or region for an AWS account is given, credentials are
used by the usual precedence of the AWS CLI: environment variables, AWS credentials file, etc.
Resources in multiple accounts and regions can be filtered and deleted by piping
the output of awsls through grep to awsrm:
$ awsls [profile/region flags] vpc -a tags | grep Name=foo | awsrm
For supported resource types and a full help text, see the README in the GitHub repository
https://github.com/jckuester/awsrm and https://github.com/jckuester/awsls.
FLAGS:
`