/
config.go
390 lines (337 loc) 路 10.8 KB
/
config.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
package config
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
g "github.com/cloudposse/atmos/pkg/globals"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var (
// Default values
defaultConfig = Configuration{
BasePath: "",
Components: Components{
Terraform: Terraform{
BasePath: "components/terraform",
ApplyAutoApprove: false,
DeployRunInit: true,
InitRunReconfigure: true,
AutoGenerateBackendFile: false,
},
Helmfile: Helmfile{
BasePath: "components/helmfile",
KubeconfigPath: "/dev/shm",
HelmAwsProfilePattern: "{namespace}-{tenant}-gbl-{stage}-helm",
ClusterNamePattern: "{namespace}-{tenant}-{environment}-{stage}-eks-cluster",
},
},
Stacks: Stacks{
BasePath: "stacks",
IncludedPaths: []string{
"**/*",
},
ExcludedPaths: []string{
"globals/**/*",
"catalog/**/*",
"**/*globals*",
},
},
Workflows: Workflows{
BasePath: "workflows",
},
Logs: Logs{
Verbose: false,
Colors: true,
},
}
// Config is the CLI configuration structure
Config Configuration
// ProcessedConfig holds all the calculated values
ProcessedConfig ProcessedConfiguration
)
// InitConfig finds and merges CLI configurations in the following order: system dir, home dir, current dir, ENV vars, command-line arguments
// https://dev.to/techschoolguru/load-config-from-file-environment-variables-in-golang-with-viper-2j2d
// https://medium.com/@bnprashanth256/reading-configuration-files-and-environment-variables-in-go-golang-c2607f912b63
func InitConfig() error {
// Config is loaded from the following locations (from lower to higher priority):
// system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
// home dir (~/.atmos)
// current directory
// ENV vars
// Command-line arguments
if Config.Initialized {
return nil
}
err := processLogsConfig()
if err != nil {
return err
}
if g.LogVerbose {
u.PrintInfo("\nProcessing and merging configurations in the following order:")
fmt.Println("system dir, home dir, current dir, ENV vars, command-line arguments")
fmt.Println()
}
v := viper.New()
v.SetConfigType("yaml")
v.SetTypeByDefaultValue(true)
// Add default config
j, err := json.Marshal(defaultConfig)
if err != nil {
return err
}
reader := bytes.NewReader(j)
err = v.MergeConfig(reader)
if err != nil {
return err
}
// Process config in system folder
configFilePath1 := ""
// https://pureinfotech.com/list-environment-variables-windows-10/
// https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables
// https://softwareengineering.stackexchange.com/questions/299869/where-is-the-appropriate-place-to-put-application-configuration-files-for-each-p
// https://stackoverflow.com/questions/37946282/why-does-appdata-in-windows-7-seemingly-points-to-wrong-folder
if runtime.GOOS == "windows" {
appDataDir := os.Getenv(g.WindowsAppDataEnvVar)
if len(appDataDir) > 0 {
configFilePath1 = appDataDir
}
} else {
configFilePath1 = g.SystemDirConfigFilePath
}
if len(configFilePath1) > 0 {
configFile1 := path.Join(configFilePath1, g.ConfigFileName)
err = processConfigFile(configFile1, v)
if err != nil {
return err
}
}
// Process config in user's HOME dir
configFilePath2, err := homedir.Dir()
if err != nil {
return err
}
configFile2 := path.Join(configFilePath2, ".atmos", g.ConfigFileName)
err = processConfigFile(configFile2, v)
if err != nil {
return err
}
// Process config in the current dir
configFilePath3, err := os.Getwd()
if err != nil {
return err
}
configFile3 := path.Join(configFilePath3, g.ConfigFileName)
err = processConfigFile(configFile3, v)
if err != nil {
return err
}
// Process config from the path in ENV var `ATMOS_CLI_CONFIG_PATH`
configFilePath4 := os.Getenv("ATMOS_CLI_CONFIG_PATH")
if len(configFilePath4) > 0 {
u.PrintInfoVerbose(fmt.Sprintf("Found ENV var ATMOS_CLI_CONFIG_PATH=%s", configFilePath4))
configFile4 := path.Join(configFilePath4, g.ConfigFileName)
err = processConfigFile(configFile4, v)
if err != nil {
return err
}
}
// https://gist.github.com/chazcheadle/45bf85b793dea2b71bd05ebaa3c28644
// https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/
err = v.Unmarshal(&Config)
if err != nil {
return err
}
Config.Initialized = true
return nil
}
// ProcessConfig processes and checks CLI configuration
func ProcessConfig(configAndStacksInfo ConfigAndStacksInfo, checkStack bool) error {
// Process ENV vars
err := processEnvVars()
if err != nil {
return err
}
// Process command-line args
err = processCommandLineArgs(configAndStacksInfo)
if err != nil {
return err
}
// Check config
err = checkConfig()
if err != nil {
return err
}
// Convert stacks base path to absolute path
stacksBasePath := path.Join(Config.BasePath, Config.Stacks.BasePath)
stacksBaseAbsPath, err := filepath.Abs(stacksBasePath)
if err != nil {
return err
}
ProcessedConfig.StacksBaseAbsolutePath = stacksBaseAbsPath
// Convert the included stack paths to absolute paths
includeStackAbsPaths, err := u.JoinAbsolutePathWithPaths(stacksBaseAbsPath, Config.Stacks.IncludedPaths)
if err != nil {
return err
}
ProcessedConfig.IncludeStackAbsolutePaths = includeStackAbsPaths
// Convert the excluded stack paths to absolute paths
excludeStackAbsPaths, err := u.JoinAbsolutePathWithPaths(stacksBaseAbsPath, Config.Stacks.ExcludedPaths)
if err != nil {
return err
}
ProcessedConfig.ExcludeStackAbsolutePaths = excludeStackAbsPaths
// Convert terraform dir to absolute path
terraformBasePath := path.Join(Config.BasePath, Config.Components.Terraform.BasePath)
terraformDirAbsPath, err := filepath.Abs(terraformBasePath)
if err != nil {
return err
}
ProcessedConfig.TerraformDirAbsolutePath = terraformDirAbsPath
// Convert helmfile dir to absolute path
helmfileBasePath := path.Join(Config.BasePath, Config.Components.Helmfile.BasePath)
helmfileDirAbsPath, err := filepath.Abs(helmfileBasePath)
if err != nil {
return err
}
ProcessedConfig.HelmfileDirAbsolutePath = helmfileDirAbsPath
// If the specified stack name is a logical name, find all stack config files in the provided paths
stackConfigFilesAbsolutePaths, stackConfigFilesRelativePaths, stackIsPhysicalPath, err := FindAllStackConfigsInPathsForStack(
configAndStacksInfo.Stack,
includeStackAbsPaths,
excludeStackAbsPaths,
)
if err != nil {
return err
}
if len(stackConfigFilesAbsolutePaths) < 1 {
j, err := yaml.Marshal(includeStackAbsPaths)
if err != nil {
return err
}
errorMessage := fmt.Sprintf("\nNo stack config files found in the provided "+
"paths:\n%s\n\nCheck if `base_path`, 'stacks.base_path', 'stacks.included_paths' and 'stacks.excluded_paths' are correctly set in CLI config "+
"files or ENV vars.", j)
return errors.New(errorMessage)
}
ProcessedConfig.StackConfigFilesAbsolutePaths = stackConfigFilesAbsolutePaths
ProcessedConfig.StackConfigFilesRelativePaths = stackConfigFilesRelativePaths
if stackIsPhysicalPath {
u.PrintInfoVerbose(fmt.Sprintf("\nThe stack '%s' matches the stack config file %s\n",
configAndStacksInfo.Stack,
stackConfigFilesRelativePaths[0]),
)
ProcessedConfig.StackType = "Directory"
} else {
// The stack is a logical name
ProcessedConfig.StackType = "Logical"
}
if g.LogVerbose {
u.PrintInfo("\nFinal CLI configuration:")
err = u.PrintAsYAML(Config)
if err != nil {
return err
}
}
return nil
}
// ProcessConfigForSpacelift processes config for Spacelift
func ProcessConfigForSpacelift() error {
// Process ENV vars
err := processEnvVars()
if err != nil {
return err
}
// Check config
err = checkConfig()
if err != nil {
return err
}
// Convert stacks base path to absolute path
stacksBasePath := path.Join(Config.BasePath, Config.Stacks.BasePath)
stacksBaseAbsPath, err := filepath.Abs(stacksBasePath)
if err != nil {
return err
}
ProcessedConfig.StacksBaseAbsolutePath = stacksBaseAbsPath
// Convert the included stack paths to absolute paths
includeStackAbsPaths, err := u.JoinAbsolutePathWithPaths(stacksBaseAbsPath, Config.Stacks.IncludedPaths)
if err != nil {
return err
}
ProcessedConfig.IncludeStackAbsolutePaths = includeStackAbsPaths
// Convert the excluded stack paths to absolute paths
excludeStackAbsPaths, err := u.JoinAbsolutePathWithPaths(stacksBaseAbsPath, Config.Stacks.ExcludedPaths)
if err != nil {
return err
}
ProcessedConfig.ExcludeStackAbsolutePaths = excludeStackAbsPaths
// Convert terraform dir to absolute path
terraformBasePath := path.Join(Config.BasePath, Config.Components.Terraform.BasePath)
terraformDirAbsPath, err := filepath.Abs(terraformBasePath)
if err != nil {
return err
}
ProcessedConfig.TerraformDirAbsolutePath = terraformDirAbsPath
// Convert helmfile dir to absolute path
helmfileBasePath := path.Join(Config.BasePath, Config.Components.Helmfile.BasePath)
helmfileDirAbsPath, err := filepath.Abs(helmfileBasePath)
if err != nil {
return err
}
ProcessedConfig.HelmfileDirAbsolutePath = helmfileDirAbsPath
// If the specified stack name is a logical name, find all stack config files in the provided paths
stackConfigFilesAbsolutePaths, stackConfigFilesRelativePaths, err := FindAllStackConfigsInPaths(
includeStackAbsPaths,
excludeStackAbsPaths,
)
if err != nil {
return err
}
if len(stackConfigFilesAbsolutePaths) < 1 {
j, err := yaml.Marshal(includeStackAbsPaths)
if err != nil {
return err
}
errorMessage := fmt.Sprintf("\nNo stack config files found in the provided "+
"paths:\n%s\n\nCheck if `base_path`, 'stacks.base_path', 'stacks.included_paths' and 'stacks.excluded_paths' are correctly set in CLI config "+
"files or ENV vars.", j)
return errors.New(errorMessage)
}
ProcessedConfig.StackConfigFilesAbsolutePaths = stackConfigFilesAbsolutePaths
ProcessedConfig.StackConfigFilesRelativePaths = stackConfigFilesRelativePaths
return nil
}
// https://github.com/NCAR/go-figure
// https://github.com/spf13/viper/issues/181
// https://medium.com/@bnprashanth256/reading-configuration-files-and-environment-variables-in-go-golang-c2607f912b63
func processConfigFile(path string, v *viper.Viper) error {
if !u.FileExists(path) {
u.PrintInfoVerbose(fmt.Sprintf("No config file found at path '%s'.", path))
return nil
}
u.PrintInfoVerbose(fmt.Sprintf("Found CLI config in '%s'", path))
reader, err := os.Open(path)
if err != nil {
return err
}
defer func(reader *os.File) {
err := reader.Close()
if err != nil {
u.PrintError(fmt.Errorf("error closing file '" + path + "'. " + err.Error()))
}
}(reader)
err = v.MergeConfig(reader)
if err != nil {
return err
}
u.PrintInfoVerbose(fmt.Sprintf("Processed CLI config '%s'", path))
return nil
}