/
utils.go
223 lines (180 loc) · 5.82 KB
/
utils.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
package cmd
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/mvisonneau/go-helpers/logger"
"github.com/mvisonneau/tfcw/pkg/functions"
"github.com/mvisonneau/tfcw/pkg/schemas"
"github.com/mvisonneau/tfcw/pkg/terraform"
"github.com/mvisonneau/tfcw/pkg/tfcw"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/zclconf/go-cty/cty/function"
)
var start time.Time
func configure(ctx *cli.Context) (c *tfcw.Client, cfg *schemas.Config, err error) {
start = ctx.App.Metadata["startTime"].(time.Time)
if err = logger.Configure(logger.Config{
Level: ctx.String("log-level"),
Format: ctx.String("log-format"),
}); err != nil {
return
}
cfg = &schemas.Config{
Runtime: schemas.Runtime{
WorkingDir: ctx.String("working-dir"),
},
}
tfcwConfigFile := computeConfigFilePath(cfg.Runtime.WorkingDir, ctx.String("config-file"))
log.Debugf("Using config file at %s", tfcwConfigFile)
// Create an EvalContext to define functions that we can use within the HCL for interpolation
evalCtx := &hcl.EvalContext{
Functions: map[string]function.Function{
"env": functions.EnvFunction,
},
}
err = hclsimple.DecodeFile(tfcwConfigFile, evalCtx, cfg)
if err != nil {
return c, cfg, fmt.Errorf("tfcw config/hcl: %s", err.Error())
}
if err = computeRuntimeConfigurationForTFC(cfg, ctx); err != nil {
return
}
c, err = tfcw.NewClient(cfg)
return
}
func exit(exitCode int, err error) cli.ExitCoder {
defer log.WithFields(
log.Fields{
"execution-time": time.Since(start),
},
).Debug("exited..")
if err != nil {
log.Error(err.Error())
}
return cli.NewExitError("", exitCode)
}
// ExecWrapper gracefully logs and exits our `run` functions
func ExecWrapper(f func(ctx *cli.Context) (int, error)) cli.ActionFunc {
return func(ctx *cli.Context) error {
return exit(f(ctx))
}
}
func computeConfigFilePath(workingDir, configFile string) string {
return strings.NewReplacer("<working-dir>", workingDir).Replace(configFile)
}
func computeRuntimeConfigurationForTFC(cfg *schemas.Config, ctx *cli.Context) (err error) {
if cfg.TFC == nil {
cfg.TFC = &schemas.TFC{}
}
if cfg.TFC.Workspace == nil {
cfg.TFC.Workspace = &schemas.Workspace{}
}
// Address
cfg.Runtime.TFC.Address, err = computeRuntimeTFCAddress(cfg.Runtime.WorkingDir, ctx.String("address"), cfg.TFC.Address)
if err != nil {
return
}
// Token
cfg.Runtime.TFC.Token, err = computeRuntimeTFCToken(cfg.Runtime.WorkingDir, ctx.String("token"), cfg.TFC.Token)
if err != nil {
return
}
// Organization
cfg.Runtime.TFC.Organization, err = computeRuntimeTFCOrganization(cfg.Runtime.WorkingDir, ctx.String("organization"), cfg.TFC.Organization)
if err != nil {
return
}
// Workspace
cfg.Runtime.TFC.Workspace, err = computeRuntimeTFCWorkspace(cfg.Runtime.WorkingDir, ctx.String("workspace"), cfg.TFC.Workspace.Name)
return
}
func computeRuntimeTFCAddress(workingDir, flagValue string, tfcwValue *string) (string, error) {
if flagValue != "" {
log.Debugf("Using TFC address '%s' from CLI flag (or env variable)", returnHTTPSPrefixedURL(flagValue))
return returnHTTPSPrefixedURL(flagValue), nil
}
if tfcwValue != nil {
log.Debugf("Using TFC address '%s' from TFCW config", returnHTTPSPrefixedURL(*tfcwValue))
return returnHTTPSPrefixedURL(*tfcwValue), nil
}
rbc, err := terraform.GetRemoteBackendConfig(workingDir)
if err != nil {
return "", err
}
if rbc != nil && rbc.Hostname != "" {
log.Debugf("Using TFC address '%s' from Terraform remote backend configuration", returnHTTPSPrefixedURL(rbc.Hostname))
return returnHTTPSPrefixedURL(rbc.Hostname), nil
}
log.Debug("Using default TFC address 'https://app.terraform.io'")
return "https://app.terraform.io", nil
}
func computeRuntimeTFCToken(workingDir, flagValue string, tfcwValue *string) (string, error) {
if flagValue != "" {
log.Debug("Using TFC token '***' from CLI flag (or env variable)")
return flagValue, nil
}
if tfcwValue != nil {
log.Debug("Using TFC token '***' from TFCW config")
return *tfcwValue, nil
}
rbc, err := terraform.GetRemoteBackendConfig(workingDir)
if err != nil {
return "", err
}
if rbc != nil && rbc.Token != "" {
log.Debug("Using TFC token '***' from Terraform remote backend configuration")
return rbc.Token, nil
}
// By not returning an empty string, we allow the TFCW to be initiated, yay!
return "_", nil
}
func computeRuntimeTFCOrganization(workingDir, flagValue string, tfcwValue *string) (string, error) {
if flagValue != "" {
log.Debugf("Using TFC organization '%s' from CLI flag (or env variable)", flagValue)
return flagValue, nil
}
if tfcwValue != nil {
log.Debugf("Using TFC organization '%s' from TFCW config", *tfcwValue)
return *tfcwValue, nil
}
rbc, err := terraform.GetRemoteBackendConfig(workingDir)
if err != nil {
return "", err
}
if rbc != nil && rbc.Organization != "" {
log.Debugf("Using TFC organization '%s' from Terraform remote backend configuration", rbc.Organization)
return rbc.Organization, nil
}
return "", nil
}
func computeRuntimeTFCWorkspace(workingDir, flagValue string, tfcwValue *string) (string, error) {
if flagValue != "" {
log.Debugf("Using TFC workspace '%s' from CLI flag (or env variable)", flagValue)
return flagValue, nil
}
if tfcwValue != nil {
log.Debugf("Using TFC workspace '%s' from TFCW config", *tfcwValue)
return *tfcwValue, nil
}
rbc, err := terraform.GetRemoteBackendConfig(workingDir)
if err != nil {
return "", err
}
if rbc != nil && rbc.Workspace != "" {
log.Debugf("Using TFC workspace '%s' from Terraform remote backend configuration", rbc.Workspace)
return rbc.Workspace, nil
}
return "", nil
}
func returnHTTPSPrefixedURL(url string) string {
re := regexp.MustCompile(`^(http|https)://`)
if re.MatchString(url) {
return url
}
return fmt.Sprintf("https://%s", url)
}