-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathload.go
327 lines (279 loc) · 8.73 KB
/
load.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
// SPDX-License-Identifier: Apache-2.0
package config
import (
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
yaml "gopkg.in/yaml.v3"
"github.com/go-vela/cli/internal"
)
// Load reads the config file and sets the values based off the provided configuration.
//
//nolint:funlen,gocyclo // ignore cyclomatic complexity and function length
func (c *Config) Load(ctx *cli.Context) error {
logrus.Debug("executing load for config file configuration")
// check if we're operating on the config resource
for _, arg := range ctx.Args().Slice() {
// check if we're operating on the config resource
if strings.EqualFold(arg, "config") {
logrus.Debugf("config arg provided in %v - skipping load for config file %s", ctx.Args().Slice(), c.File)
return nil
}
}
// use custom filesystem which enables us to test
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero
a := &afero.Afero{
Fs: appFS,
}
// send Filesystem call to read config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero.ReadFile
data, err := a.ReadFile(c.File)
if err != nil {
return err
}
// create the config file object
//
// https://pkg.go.dev/github.com/go-vela/cli/action/config?tab=doc#ConfigFile
config := new(ConfigFile)
// update the config object with the current content
//
// https://pkg.go.dev/gopkg.in/yaml.v3?tab=doc#Unmarshal
err = yaml.Unmarshal(data, config)
if err != nil {
return err
}
// check if the config file is empty
//
// https://pkg.go.dev/github.com/go-vela/cli/action/config?tab=doc#ConfigFile.Empty
if config.Empty() {
logrus.Warningf("empty/unsupported config loaded from %s - fix this with `vela generate config`", c.File)
return nil
}
// capture a list of all available flags to set in the current context
flags := []cli.Flag{}
// check if the app is provided in the context
if ctx.App != nil {
// append the flags from the app provided
flags = append(flags, ctx.App.VisibleFlags()...)
}
// check if the command is provided in the context
if ctx.Command != nil {
// append the flags from the context provided
flags = append(flags, ctx.Command.VisibleFlags()...)
}
// iterate through all available flags in the current context
for _, flag := range flags {
// capture string value for flag
f := strings.Join(flag.Names(), " ")
// check if the API address flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagAPIAddress) &&
!ctx.IsSet(internal.FlagAPIAddress) &&
len(config.API.Address) > 0 {
// set the API address field to value from config
err = ctx.Set(internal.FlagAPIAddress, config.API.Address)
if err != nil {
return err
}
continue
}
// check if the API token flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagAPIToken) &&
!ctx.IsSet(internal.FlagAPIToken) &&
len(config.API.Token) > 0 {
// set the API token field to value from config
err = ctx.Set(internal.FlagAPIToken, config.API.Token)
if err != nil {
return err
}
continue
}
// check if the API access token flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagAPIAccessToken) &&
!ctx.IsSet(internal.FlagAPIAccessToken) &&
len(config.API.AccessToken) > 0 {
// set the API access token field to value from config
err = ctx.Set(internal.FlagAPIAccessToken, config.API.AccessToken)
if err != nil {
return err
}
continue
}
// check if the API refresh token flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagAPIRefreshToken) &&
!ctx.IsSet(internal.FlagAPIRefreshToken) &&
len(config.API.RefreshToken) > 0 {
// set the API refresh token field to value from config
err = ctx.Set(internal.FlagAPIRefreshToken, config.API.RefreshToken)
if err != nil {
return err
}
continue
}
// check if the API version flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagAPIVersion) &&
!ctx.IsSet(internal.FlagAPIVersion) &&
len(config.API.Version) > 0 {
// set the API version field to value from config
err = ctx.Set(internal.FlagAPIVersion, config.API.Version)
if err != nil {
return err
}
continue
}
// check if the log level flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagLogLevel) &&
!ctx.IsSet(internal.FlagLogLevel) &&
len(config.Log.Level) > 0 {
// set the log level field to value from config
err = ctx.Set(internal.FlagLogLevel, config.Log.Level)
if err != nil {
return err
}
}
// check if the git sync flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagNoGit) &&
!ctx.IsSet(internal.FlagNoGit) &&
len(config.NoGit) > 0 {
err = ctx.Set(internal.FlagNoGit, config.NoGit)
if err != nil {
return err
}
}
// check if the output flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagOutput) &&
!ctx.IsSet(internal.FlagOutput) &&
len(config.Output) > 0 {
// set the output field to value from config
err = ctx.Set(internal.FlagOutput, config.Output)
if err != nil {
return err
}
continue
}
// check if the color flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagColor) &&
!ctx.IsSet(internal.FlagColor) {
// set the color field to value from config
c := "true"
if config.Color != nil && !*config.Color {
c = "false"
}
err = ctx.Set(internal.FlagColor, c)
if err != nil {
return err
}
continue
}
// check if the color format flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagColorFormat) &&
!ctx.IsSet(internal.FlagColorFormat) &&
len(config.ColorFormat) > 0 {
// set the color format field to value from config
err = ctx.Set(internal.FlagColorFormat, config.ColorFormat)
if err != nil {
return err
}
continue
}
// check if the color theme flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagColorTheme) &&
!ctx.IsSet(internal.FlagColorTheme) &&
len(config.ColorTheme) > 0 {
// set the color theme field to value from config
err = ctx.Set(internal.FlagColorTheme, config.ColorTheme)
if err != nil {
return err
}
continue
}
// check if the org flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagOrg) &&
!ctx.IsSet(internal.FlagOrg) &&
len(config.Org) > 0 {
// set the org field to value from config
err = ctx.Set(internal.FlagOrg, config.Org)
if err != nil {
return err
}
continue
}
// check if the repo flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagRepo) &&
!ctx.IsSet(internal.FlagRepo) &&
len(config.Repo) > 0 {
// set the repo field to value from config
err = ctx.Set(internal.FlagRepo, config.Repo)
if err != nil {
return err
}
continue
}
// check if the secret engine flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagSecretEngine) &&
!ctx.IsSet(internal.FlagSecretEngine) &&
len(config.Secret.Engine) > 0 {
// set the secret engine field to value from config
err = ctx.Set(internal.FlagSecretEngine, config.Secret.Engine)
if err != nil {
return err
}
continue
}
// check if the secret type flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagSecretType) &&
!ctx.IsSet(internal.FlagSecretType) &&
len(config.Secret.Type) > 0 {
// set the secret type field to value from config
err = ctx.Set(internal.FlagSecretType, config.Secret.Type)
if err != nil {
return err
}
continue
}
// check if the compiler github token flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagCompilerGitHubToken) &&
!ctx.IsSet(internal.FlagCompilerGitHubToken) &&
config.Compiler != nil &&
len(config.Compiler.GitHub.Token) > 0 {
// set the compiler github token field to value from config
err = ctx.Set(internal.FlagCompilerGitHubToken, config.Compiler.GitHub.Token)
if err != nil {
return err
}
continue
}
// check if the compiler github url flag is available
// and if it is set in the context
if strings.Contains(f, internal.FlagCompilerGitHubURL) &&
!ctx.IsSet(internal.FlagCompilerGitHubURL) &&
config.Compiler != nil &&
len(config.Compiler.GitHub.URL) > 0 {
// set the compiler github url field to value from config
err = ctx.Set(internal.FlagCompilerGitHubURL, config.Compiler.GitHub.URL)
if err != nil {
return err
}
continue
}
}
return nil
}