forked from gruntwork-io/terragrunt
/
args.go
461 lines (394 loc) · 15.6 KB
/
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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/go-version"
"github.com/urfave/cli"
)
// Parse command line options that are passed in for Terragrunt
func ParseTerragruntOptions(cliContext *cli.Context) (*options.TerragruntOptions, error) {
terragruntOptions, err := parseTerragruntOptionsFromArgs(cliContext.App.Version, cliContext.Args(), cliContext.App.Writer, cliContext.App.ErrWriter)
if err != nil {
return nil, err
}
return terragruntOptions, nil
}
// TODO: replace the urfave CLI library with something else.
//
// EXPLANATION: The normal way to parse flags with the urfave CLI library would be to define the flags in the
// CreateTerragruntCLI method and to read the values of those flags using cliContext.String(...),
// cliContext.Bool(...), etc. Unfortunately, this does not work here due to a limitation in the urfave
// CLI library: if the user passes in any "command" whatsoever, (e.g. the "apply" in "terragrunt apply"), then
// any flags that come after it are not parsed (e.g. the "--foo" is not parsed in "terragrunt apply --foo").
// Therefore, we have to parse options ourselves, which is infuriating. For more details on this limitation,
// see: https://github.com/urfave/cli/issues/533. For now, our workaround is to dumbly loop over the arguments
// and look for the ones we need, but in the future, we should change to a different CLI library to avoid this
// limitation.
func parseTerragruntOptionsFromArgs(terragruntVersion string, args []string, writer, errWriter io.Writer) (*options.TerragruntOptions, error) {
defaultWorkingDir := os.Getenv("TERRAGRUNT_WORKING_DIR")
if defaultWorkingDir == "" {
currentDir, err := os.Getwd()
if err != nil {
return nil, errors.WithStackTrace(err)
}
defaultWorkingDir = currentDir
}
workingDir, err := parseStringArg(args, OPT_WORKING_DIR, defaultWorkingDir)
if err != nil {
return nil, err
}
downloadDirRaw, err := parseStringArg(args, OPT_DOWNLOAD_DIR, os.Getenv("TERRAGRUNT_DOWNLOAD"))
if err != nil {
return nil, err
}
if downloadDirRaw == "" {
downloadDirRaw = util.JoinPath(workingDir, options.TerragruntCacheDir)
}
downloadDir, err := filepath.Abs(downloadDirRaw)
if err != nil {
return nil, errors.WithStackTrace(err)
}
terragruntConfigPath, err := parseStringArg(args, OPT_TERRAGRUNT_CONFIG, os.Getenv("TERRAGRUNT_CONFIG"))
if err != nil {
return nil, err
}
if terragruntConfigPath == "" {
terragruntConfigPath = config.GetDefaultConfigPath(workingDir)
}
terragruntHclFilePath, err := parseStringArg(args, OPT_TERRAGRUNT_HCLFMT_FILE, "")
if err != nil {
return nil, err
}
awsProviderPatchOverrides, err := parseMutliStringKeyValueArg(args, OPT_TERRAGRUNT_OVERRIDE_ATTR, nil)
if err != nil {
return nil, err
}
terraformPath, err := parseStringArg(args, OPT_TERRAGRUNT_TFPATH, os.Getenv("TERRAGRUNT_TFPATH"))
if err != nil {
return nil, err
}
if terraformPath == "" {
terraformPath = options.TERRAFORM_DEFAULT_PATH
}
terraformSource, err := parseStringArg(args, OPT_TERRAGRUNT_SOURCE, os.Getenv("TERRAGRUNT_SOURCE"))
if err != nil {
return nil, err
}
sourceUpdate := parseBooleanArg(args, OPT_TERRAGRUNT_SOURCE_UPDATE, os.Getenv("TERRAGRUNT_SOURCE_UPDATE") == "true" || os.Getenv("TERRAGRUNT_SOURCE_UPDATE") == "1")
ignoreDependencyErrors := parseBooleanArg(args, OPT_TERRAGRUNT_IGNORE_DEPENDENCY_ERRORS, false)
ignoreDependencyOrder := parseBooleanArg(args, OPT_TERRAGRUNT_IGNORE_DEPENDENCY_ORDER, false)
ignoreExternalDependencies := parseBooleanArg(args, OPT_TERRAGRUNT_IGNORE_EXTERNAL_DEPENDENCIES, false)
includeExternalDependencies := parseBooleanArg(args, OPT_TERRAGRUNT_INCLUDE_EXTERNAL_DEPENDENCIES, false)
iamRole, err := parseStringArg(args, OPT_TERRAGRUNT_IAM_ROLE, os.Getenv("TERRAGRUNT_IAM_ROLE"))
if err != nil {
return nil, err
}
excludeDirs, err := parseMultiStringArg(args, OPT_TERRAGRUNT_EXCLUDE_DIR, []string{})
if err != nil {
return nil, err
}
includeDirs, err := parseMultiStringArg(args, OPT_TERRAGRUNT_INCLUDE_DIR, []string{})
if err != nil {
return nil, err
}
strictInclude := parseBooleanArg(args, OPT_TERRAGRUNT_STRICT_INCLUDE, false)
debug := parseBooleanArg(args, OPT_TERRAGRUNT_DEBUG, false)
// Those correspond to logrus levels
logLevel, err := parseStringArg(args, OPT_TERRAGRUNT_LOGLEVEL, options.DEFAULT_LOG_LEVEL)
if err != nil {
return nil, err
}
if debug {
logLevel = "debug"
}
opts, err := options.NewTerragruntOptions(filepath.ToSlash(terragruntConfigPath))
if err != nil {
return nil, err
}
envValue, envProvided := os.LookupEnv("TERRAGRUNT_PARALLELISM")
parallelism, err := parseIntArg(args, OPT_TERRAGRUNT_PARALLELISM, envValue, envProvided, options.DEFAULT_PARALLELISM)
if err != nil {
return nil, err
}
opts.TerraformPath = filepath.ToSlash(terraformPath)
opts.AutoInit = !parseBooleanArg(args, OPT_TERRAGRUNT_NO_AUTO_INIT, os.Getenv("TERRAGRUNT_AUTO_INIT") == "false")
opts.AutoRetry = !parseBooleanArg(args, OPT_TERRAGRUNT_NO_AUTO_RETRY, os.Getenv("TERRAGRUNT_AUTO_RETRY") == "false")
opts.NonInteractive = parseBooleanArg(args, OPT_NON_INTERACTIVE, os.Getenv("TF_INPUT") == "false" || os.Getenv("TF_INPUT") == "0")
opts.TerraformCliArgs = filterTerragruntArgs(args)
opts.OriginalTerraformCommand = util.FirstArg(opts.TerraformCliArgs)
opts.TerraformCommand = util.FirstArg(opts.TerraformCliArgs)
opts.WorkingDir = filepath.ToSlash(workingDir)
opts.DownloadDir = filepath.ToSlash(downloadDir)
opts.Logger = util.CreateLogEntryWithWriter(errWriter, "", logLevel)
opts.RunTerragrunt = RunTerragrunt
opts.Source = terraformSource
opts.SourceUpdate = sourceUpdate
opts.TerragruntVersion, err = version.NewVersion(terragruntVersion)
if err != nil {
// Malformed Terragrunt version; set the version to 0.0
opts.TerragruntVersion, err = version.NewVersion("0.0")
if err != nil {
return nil, err
}
}
opts.IgnoreDependencyErrors = ignoreDependencyErrors
opts.IgnoreDependencyOrder = ignoreDependencyOrder
opts.IgnoreExternalDependencies = ignoreExternalDependencies
opts.IncludeExternalDependencies = includeExternalDependencies
opts.Writer = writer
opts.ErrWriter = errWriter
opts.Env = parseEnvironmentVariables(os.Environ())
opts.IamRole = iamRole
opts.ExcludeDirs = excludeDirs
opts.IncludeDirs = includeDirs
opts.StrictInclude = strictInclude
opts.Parallelism = parallelism
opts.Check = parseBooleanArg(args, OPT_TERRAGRUNT_CHECK, os.Getenv("TERRAGRUNT_CHECK") == "true")
opts.HclFile = filepath.ToSlash(terragruntHclFilePath)
opts.AwsProviderPatchOverrides = awsProviderPatchOverrides
return opts, nil
}
func filterTerraformExtraArgs(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) []string {
out := []string{}
cmd := util.FirstArg(terragruntOptions.TerraformCliArgs)
for _, arg := range terragruntConfig.Terraform.ExtraArgs {
for _, arg_cmd := range arg.Commands {
if cmd == arg_cmd {
lastArg := util.LastArg(terragruntOptions.TerraformCliArgs)
skipVars := (cmd == "apply" || cmd == "destroy") && util.IsFile(lastArg)
// The following is a fix for GH-493.
// If the first argument is "apply" and the second argument is a file (plan),
// we don't add any -var-file to the command.
if arg.Arguments != nil {
if skipVars {
// If we have to skip vars, we need to iterate over all elements of array...
for _, a := range *arg.Arguments {
if !strings.HasPrefix(a, "-var") {
out = append(out, a)
}
}
} else {
// ... Otherwise, let's add all the arguments
out = append(out, *arg.Arguments...)
}
}
if !skipVars {
// If RequiredVarFiles is specified, add -var-file=<file> for each specified files
if arg.RequiredVarFiles != nil {
for _, file := range util.RemoveDuplicatesFromListKeepLast(*arg.RequiredVarFiles) {
out = append(out, fmt.Sprintf("-var-file=%s", file))
}
}
// If OptionalVarFiles is specified, check for each file if it exists and if so, add -var-file=<file>
// It is possible that many files resolve to the same path, so we remove duplicates.
if arg.OptionalVarFiles != nil {
for _, file := range util.RemoveDuplicatesFromListKeepLast(*arg.OptionalVarFiles) {
if util.FileExists(file) {
out = append(out, fmt.Sprintf("-var-file=%s", file))
} else {
terragruntOptions.Logger.Debugf("Skipping var-file %s as it does not exist", file)
}
}
}
}
}
}
}
return out
}
func filterTerraformEnvVarsFromExtraArgs(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) map[string]string {
out := map[string]string{}
cmd := util.FirstArg(terragruntOptions.TerraformCliArgs)
for _, arg := range terragruntConfig.Terraform.ExtraArgs {
if arg.EnvVars == nil {
continue
}
for _, argcmd := range arg.Commands {
if cmd == argcmd {
for k, v := range *arg.EnvVars {
out[k] = v
}
}
}
}
return out
}
func parseEnvironmentVariables(environment []string) map[string]string {
environmentMap := make(map[string]string)
for i := 0; i < len(environment); i++ {
variableSplit := strings.SplitN(environment[i], "=", 2)
if len(variableSplit) == 2 {
environmentMap[strings.TrimSpace(variableSplit[0])] = variableSplit[1]
}
}
return environmentMap
}
// Return a copy of the given args with all Terragrunt-specific args removed
func filterTerragruntArgs(args []string) []string {
out := []string{}
for i := 0; i < len(args); i++ {
arg := args[i]
argWithoutPrefix := strings.TrimPrefix(arg, "--")
if util.ListContainsElement(MULTI_MODULE_COMMANDS, arg) {
// Skip multi-module commands entirely
continue
}
if util.ListContainsElement(ALL_TERRAGRUNT_STRING_OPTS, argWithoutPrefix) {
// String flags have the argument and the value, so skip both
i = i + 1
continue
}
if util.ListContainsElement(ALL_TERRAGRUNT_BOOLEAN_OPTS, argWithoutPrefix) {
// Just skip the boolean flag
continue
}
out = append(out, arg)
}
return out
}
// isDeprecatedOption checks if provided option is deprecated, and returns its substitution
// TODO: ideally, it would be better to make this return (string, err)
func isDeprecatedOption(optionName string) string {
newOption, deprecated := DEPRECATED_ARGUMENTS[optionName]
if deprecated {
logger := util.CreateLogEntry("", "debug")
logger.Warnf("Command line option %s is deprecated, please consider using %s", optionName, newOption)
return newOption
}
return optionName
}
// Find a boolean argument (e.g. --foo) of the given name in the given list of arguments. If it's present, return true.
// If it isn't, return defaultValue.
func parseBooleanArg(args []string, argName string, defaultValue bool) bool {
for _, arg := range args {
if arg == fmt.Sprintf("--%s", argName) {
argName = isDeprecatedOption(argName)
return true
}
}
return defaultValue
}
// Find a string argument (e.g. --foo "VALUE") of the given name in the given list of arguments. If it's present,
// return its value. If it is present, but has no value, return an error. If it isn't present, return defaultValue.
func parseStringArg(args []string, argName string, defaultValue string) (string, error) {
for i, arg := range args {
if arg == fmt.Sprintf("--%s", argName) {
argName = isDeprecatedOption(argName)
if (i + 1) < len(args) {
return args[i+1], nil
} else {
return "", errors.WithStackTrace(ArgMissingValue(argName))
}
}
}
return defaultValue, nil
}
// Find a int argument (e.g. --foo 1) of the given name in the given list of arguments. If it's present,
// return its value. If it is present, but has no value, return an error. If it isn't present, return envValue if provided. If not provided, return defaultValue.
func parseIntArg(args []string, argName string, envValue string, envProvided bool, defaultValue int) (int, error) {
for i, arg := range args {
if arg == fmt.Sprintf("--%s", argName) {
argName = isDeprecatedOption(argName)
if (i + 1) < len(args) {
return strconv.Atoi(args[i+1])
} else {
return 0, errors.WithStackTrace(ArgMissingValue(argName))
}
}
}
if envProvided {
return strconv.Atoi(envValue)
} else {
return defaultValue, nil
}
}
// Find multiple string arguments of the same type (e.g. --foo "VALUE_A" --foo "VALUE_B") of the given name in the given list of arguments. If there are any present,
// return a list of all values. If there are any present, but one of them has no value, return an error. If there aren't any present, return defaultValue.
func parseMultiStringArg(args []string, argName string, defaultValue []string) ([]string, error) {
stringArgs := []string{}
for i, arg := range args {
if arg == fmt.Sprintf("--%s", argName) {
argName = isDeprecatedOption(argName)
if (i + 1) < len(args) {
stringArgs = append(stringArgs, args[i+1])
} else {
return nil, errors.WithStackTrace(ArgMissingValue(argName))
}
}
}
if len(stringArgs) == 0 {
return defaultValue, nil
}
return stringArgs, nil
}
// Find multiple key=vallue arguments of the same type (e.g. --foo "KEY_A=VALUE_A" --foo "KEY_B=VALUE_B") of the given name in the given list of arguments. If there are any present,
// return a map of all values. If there are any present, but one of them has no value, return an error. If there aren't any present, return defaultValue.
func parseMutliStringKeyValueArg(args []string, argName string, defaultValue map[string]string) (map[string]string, error) {
asList, err := parseMultiStringArg(args, argName, nil)
if err != nil {
return nil, err
}
if asList == nil {
return defaultValue, nil
}
asMap := map[string]string{}
for _, arg := range asList {
parts := strings.Split(arg, "=")
if len(parts) != 2 {
return nil, errors.WithStackTrace(InvalidKeyValue(arg))
}
key := parts[0]
value := parts[1]
asMap[key] = value
}
return asMap, nil
}
// Convert the given variables to a map of environment variables that will expose those variables to Terraform. The
// keys will be of the format TF_VAR_xxx and the values will be converted to JSON, which Terraform knows how to read
// natively.
func toTerraformEnvVars(vars map[string]interface{}) (map[string]string, error) {
out := map[string]string{}
for varName, varValue := range vars {
envVarName := fmt.Sprintf("TF_VAR_%s", varName)
envVarValue, err := asTerraformEnvVarJsonValue(varValue)
if err != nil {
return nil, err
}
out[envVarName] = string(envVarValue)
}
return out, nil
}
// Convert the given value to a JSON value that can be passed to Terraform as an environment variable. For the most
// part, this converts the value directly to JSON using Go's built-in json.Marshal. However, we have special handling
// for strings, which with normal JSON conversion would be wrapped in quotes, but when passing them to Terraform via
// env vars, we need to NOT wrap them in quotes, so this method adds special handling for that case.
func asTerraformEnvVarJsonValue(value interface{}) (string, error) {
switch val := value.(type) {
case string:
return val, nil
default:
envVarValue, err := json.Marshal(val)
if err != nil {
return "", errors.WithStackTrace(err)
}
return string(envVarValue), nil
}
}
// Custom error types
type ArgMissingValue string
func (err ArgMissingValue) Error() string {
return fmt.Sprintf("You must specify a value for the --%s option", string(err))
}
type InvalidKeyValue string
func (err InvalidKeyValue) Error() string {
return fmt.Sprintf("Invalid key-value pair. Expected format KEY=VALUE, got %s.", string(err))
}