-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (70 loc) · 2.39 KB
/
main.go
File metadata and controls
91 lines (70 loc) · 2.39 KB
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
package main
import (
"context"
"os"
"github.com/gruntwork-io/terragrunt/internal/cli"
"github.com/gruntwork-io/terragrunt/internal/cli/flags/global"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/internal/shell"
"github.com/gruntwork-io/terragrunt/internal/tf"
"github.com/gruntwork-io/terragrunt/internal/util"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/pkg/log/format"
"github.com/gruntwork-io/terragrunt/pkg/options"
)
// The main entrypoint for Terragrunt
func main() {
exitCode := tf.NewDetailedExitCodeMap()
opts := options.NewTerragruntOptions()
l := log.New(
log.WithOutput(opts.Writers.ErrWriter),
log.WithLevel(options.DefaultLogLevel),
log.WithFormatter(format.NewFormatter(format.NewPrettyFormatPlaceholders())),
)
// Immediately parse the `TG_LOG_LEVEL` environment variable, e.g. to set the TRACE level.
if err := global.NewLogLevelFlag(l, opts, nil).Parse(os.Args); err != nil {
l.Error(err.Error())
os.Exit(1)
}
defer func() {
if opts.TerraformCliArgs.Contains(tf.FlagNameDetailedExitCode) {
errors.Recover(checkForErrorsAndExit(l, exitCode.GetFinalDetailedExitCode()))
return
}
errors.Recover(checkForErrorsAndExit(l, exitCode.GetFinalExitCode()))
}()
app := cli.NewApp(l, opts)
ctx := setupContext(l, exitCode)
err := app.RunContext(ctx, os.Args)
if opts.TerraformCliArgs.Contains(tf.FlagNameDetailedExitCode) {
checkForErrorsAndExit(l, exitCode.GetFinalDetailedExitCode())(err)
return
}
checkForErrorsAndExit(l, exitCode.GetFinalExitCode())(err)
}
// If there is an error, display it in the console and exit with a non-zero exit code. Otherwise, exit 0.
func checkForErrorsAndExit(l log.Logger, exitCode int) func(error) {
return func(err error) {
if err == nil {
os.Exit(exitCode)
}
l.Error(err.Error())
if errStack := errors.ErrorStack(err); errStack != "" {
l.Trace(errStack)
}
// exit with the underlying error code
exitCoder, exitCodeErr := util.GetExitCode(err)
if exitCodeErr != nil {
exitCoder = 1
}
if explain := shell.ExplainError(err); len(explain) > 0 {
l.Errorf("Suggested fixes: \n%s", explain)
}
os.Exit(exitCoder)
}
}
func setupContext(l log.Logger, exitCode *tf.DetailedExitCodeMap) context.Context {
ctx := context.Background()
ctx = tf.ContextWithDetailedExitCode(ctx, exitCode)
return log.ContextWithLogger(ctx, l)
}