-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandLine.go
162 lines (137 loc) · 5.53 KB
/
commandLine.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/******************************************************************************
Cloud Resource Counter
File: commandLine.go
Summary: Retrieve account ID (assumed to be a single value) for the current
user session.
******************************************************************************/
package main
import (
"flag"
"os"
"github.com/aws/aws-sdk-go/aws/session"
color "github.com/logrusorgru/aurora"
)
// CommandLineSettings defines the command line settings supplied by
// the caller.
type CommandLineSettings struct {
// Profile related settings
profileName string
defaultProfileName string
useSSO bool
// Region related settings
allRegions bool
regionName string
// Output (CSV) file
outputFileName string
outputFile *os.File
appendToOutput bool
noOutputFile bool
// Trace file
traceFileName string
traceFile *os.File
}
// Process inspects the command line for valid arguments.
//
// Usage of aws-resource-counter
// --sso: Use SSO for authentication
// --output-file OF: Write the results to file OF. Defaults to 'resources.csv'
// --no-output: If set, then the results are not saved to any file.
// --profile PN: Use the credentials associated with shared profile PN
// --region RN: View resource counts for the AWS region RN
// --trace-file TF: Create a trace file that contains all calls to AWS.
// --version: Display version information
//
func (cls *CommandLineSettings) Process(args []string, am ActivityMonitor) func() {
var showVersion bool
emptyFn := func() {}
// What is our default profile?
if cls.defaultProfileName = os.Getenv("AWS_PROFILE"); cls.defaultProfileName == "" {
cls.defaultProfileName = session.DefaultSharedConfigProfile
}
// Define a new FlagSet
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Define and parse the command line arguments...
flagSet.BoolVar(&cls.useSSO, "sso", false, "Use SSO for authentication (default false)")
flagSet.StringVar(&cls.outputFileName, "output-file", "", "CSV Output File. Specify a path to a `file` to save the generated CSV file. (default resources.csv)")
flagSet.BoolVar(&cls.noOutputFile, "no-output", false, "Do not save the results of this run into any file. (default false--save results to a file)")
flagSet.StringVar(&cls.profileName, "profile", cls.defaultProfileName, "The name of the AWS Profile to use.")
flagSet.StringVar(&cls.regionName, "region", "", "The name of the AWS Region to use. If omitted, then all regions will be examined. This is the default behavior.")
flagSet.StringVar(&cls.traceFileName, "trace-file", "", "AWS Trace Log. Specify a `file` to record API calls being made. Each subsequent run OVERWRITES the prior run.")
flagSet.BoolVar(&showVersion, "version", false, "Shows the version number.")
flagSet.Parse(args)
// Check for a valid AWS Region
if cls.regionName != "" {
// If not valid region name, then get out now...
if !IsValidRegionName(cls.regionName) {
am.ActionError("Error: '%s' is not a valid AWS Region name.", cls.regionName)
return emptyFn
}
} else {
// Record that all regions are being examined
cls.allRegions = true
}
// If both --output-file and --no-output specified, then complain
if cls.outputFileName != "" && cls.noOutputFile {
// Show error...
am.ActionError("Error: Cannot specify both --output-file and -no-output!")
return emptyFn
}
// If no output file specified, then use a default name (assuming that we are not barring output)
if cls.outputFileName == "" && !cls.noOutputFile {
// Set the default output file
cls.outputFileName = "resources.csv"
}
// Did the user just want to see the version?
if showVersion {
am.Message("%s, version %s (built %s)\n", "Cloud Resource Counter", version, date)
am.Exit(0)
return emptyFn
}
// Check whether a response file is being specified
if cls.outputFileName != "" && !cls.noOutputFile {
// Determine whether to append the output file or not
cls.appendToOutput = FileExists(cls.outputFileName)
// Try to open the file for writing
cls.outputFile = OpenFileForWriting(cls.outputFileName, "CSV", am, cls.appendToOutput)
}
// Check whether a trace file is being specified
if cls.traceFileName != "" {
// Try to open the file for writing
cls.traceFile = OpenFileForWriting(cls.traceFileName, "trace", am, false)
}
// Return a function that we can use for cleaning up open resources
return func() {
if !NilInterface(cls.outputFile) {
cls.outputFile.Close()
}
if !NilInterface(cls.traceFile) {
cls.traceFile.Close()
}
}
}
// Display constructs a listing of all command line settings to the Activity Monitor
func (cls *CommandLineSettings) Display(am ActivityMonitor) {
// What is the region being selected?
var displayRegionName string
if cls.regionName == "" {
displayRegionName = "(All regions supported by this account)"
} else {
displayRegionName = cls.regionName
}
// What is the file name for the output file
var displayOutputFile string
if cls.outputFileName == "" {
displayOutputFile = "(none)"
} else {
displayOutputFile = cls.outputFileName
}
// Output information about utility running
am.Message("%s (v%s) running with:\n", color.Bold("Cloud Resource Counter"), version)
am.Message(" o %s: %s\n", color.Italic("AWS Profile"), cls.profileName)
am.Message(" o %s: %s\n", color.Italic("AWS Region"), displayRegionName)
am.Message(" o %s: %s\n", color.Italic("Output file"), displayOutputFile)
// Are we tracing?
if cls.traceFileName != "" {
am.Message(" o %s: %s\n", color.Italic("Trace file"), cls.traceFileName)
}
}