-
Notifications
You must be signed in to change notification settings - Fork 90
/
config.go
432 lines (362 loc) · 13.6 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
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
/*
Copyright 2023 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import (
"context"
"errors"
"fmt"
"os"
"path"
"strings"
"time"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
validator "github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
"github.com/gofrs/flock"
"github.com/spf13/viper"
"golang.org/x/text/cases"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
)
// EnvironmentKey is the key used for the environment section
const (
ApplicationKey string = "application"
WorkspacesKey string = "workspaces"
)
type WorkspaceSection struct {
Default string `json:"default" mapstructure:"default" yaml:"default"`
Items map[string]workspaces.Workspace `json:"items" mapstructure:"items" yaml:"items" validate:"dive"`
}
// HasWorkspace returns true if the specified workspace already exists. This function ignores the default workspace.
func (ws WorkspaceSection) HasWorkspace(name string) bool {
_, ok := ws.Items[cases.Fold().String(name)]
return ok
}
// GetWorkspace returns the specified workspace or the default workspace if 'name' is empty.
//
// GetWorkspace checks if the given workspace name is empty and if so, checks if a default workspace is set. If a
// workspace name is provided, it looks up the workspace in the Items map and returns it. If the workspace does not exist,
// it returns an error.
func (ws WorkspaceSection) GetWorkspace(name string) (*workspaces.Workspace, error) {
if name == "" && ws.Default == "" {
return nil, nil
} else if name == "" {
name = ws.Default
}
result, ok := ws.Items[cases.Fold().String(name)]
if !ok {
return nil, fmt.Errorf("the workspace '%v' does not exist. use `rad init` or `rad workspace create` and try again", name)
}
return &result, nil
}
// ReadWorkspaceSection reads the WorkspaceSection from radius config.
//
// ReadWorkspaceSection reads the WorkspaceSection from the given viper instance, validates it and returns it. If the
// WorkspaceSection is not present, an empty one is returned. If any errors occur during validation, an error is returned.
func ReadWorkspaceSection(v *viper.Viper) (WorkspaceSection, error) {
section := WorkspaceSection{}
s := v.Sub(WorkspacesKey)
if s == nil {
// This may happen if the key was set directly to one of our structs, so let's try reading
// that.
obj := v.Get(WorkspacesKey)
if obj == nil {
// OK really nil, return a blank config.
return WorkspaceSection{Items: map[string]workspaces.Workspace{}}, nil
}
s, ok := obj.(WorkspaceSection)
if !ok {
return WorkspaceSection{}, fmt.Errorf("failed to read the config file: %s", v.ConfigFileUsed())
}
section = s
} else {
err := s.UnmarshalExact(§ion)
if err != nil {
return WorkspaceSection{}, err
}
}
// if items is not present it will be nil
if section.Items == nil {
section.Items = map[string]workspaces.Workspace{}
}
for name, ws := range section.Items {
copy := ws
// The names of the workspace aren't serialized to the configuration in the same
// way, so set the field here.
copy.Name = name
// We also want to make it clear these workspaces came from the per-user (config.yaml)
// file.
copy.Source = workspaces.SourceUserConfig
section.Items[name] = copy
}
err := validate(section)
if err != nil {
return WorkspaceSection{}, err
}
return section, nil
}
// UpdateWorkspaceSection updates the WorkspacesKey in the given viper instance with the given WorkspaceSection.
func UpdateWorkspaceSection(v *viper.Viper, section WorkspaceSection) {
v.Set(WorkspacesKey, section)
}
// HasWorkspace returns true if the specified workspace already exists. This function ignores the default workspace.
//
// HasWorkspace reads the workspace section from the given Viper instance and checks if it contains a workspace with the
// given name. If an error occurs while reading the workspace section, it is returned to the caller.
func HasWorkspace(v *viper.Viper, name string) (bool, error) {
section, err := ReadWorkspaceSection(v)
if err != nil {
return false, err
}
return section.HasWorkspace(name), nil
}
// GetWorkspace returns the specified workspace or the default workspace in configuration if 'name' is empty.
//
// GetWorkspace reads the workspace section from the viper configuration and returns the workspace with the given name, or
// an error if the workspace does not exist or there was an issue reading the configuration.
func GetWorkspace(v *viper.Viper, name string) (*workspaces.Workspace, error) {
section, err := ReadWorkspaceSection(v)
if err != nil {
return nil, err
}
return section.GetWorkspace(name)
}
func getConfig(configFilePath string) (*viper.Viper, error) {
config := viper.New()
if configFilePath == "" {
// Set config file using the HOME directory.
// This is extremely unlikely to fail on us. This would only happen
// if the user has no HOME (or USERPROFILE on Windows) directory.
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to find the user's home directory: %w", err)
}
rad := path.Join(home, ".rad")
config.AddConfigPath(rad)
config.SetConfigName("config")
} else {
config.SetConfigFile(configFilePath)
}
return config, nil
}
// Create a config if its not present
func createConfigFile(configFilePath string) error {
dir := path.Dir(configFilePath)
_, err := os.Stat(dir)
if os.IsNotExist(err) {
err := os.MkdirAll(dir, os.ModeDir|0755)
if err != nil {
return fmt.Errorf("failed to create directory '%s': %w", dir, err)
}
} else if err != nil {
return fmt.Errorf("failed to find directory '%s': %w", dir, err)
}
return nil
}
// LoadConfigNoLock reads in a configuration file from the given path and creates it if it doesn't exist. It handles errors
//
// if the file is not found or if there is an issue reading it.
func LoadConfigNoLock(configFilePath string) (*viper.Viper, error) {
config, err := getConfig(configFilePath)
if err != nil {
return nil, err
}
configFile, err := GetConfigFilePath(config)
if err != nil {
return nil, err
}
// On Ubuntu OS, getConfig() function doesnt create a config file if its not present.
err = createConfigFile(configFile)
if err != nil {
return nil, err
}
err = config.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// It's ok the config file is not found, this could be the first time the CLI
// is running. Commands that require configuration will check for the data they need.
} else if os.IsNotExist(err) {
// It's ok the config file is not found, this could be the first time the CLI
// is running. Commands that require configuration will check for the data they need.
} else if err != nil {
return nil, err
}
return config, nil
}
// LoadConfig() attempts to read a configuration file from the given path and acquire a shared lock on it. If the file does
//
// not exist, it will be created. If the lock cannot be acquired, an error will be returned.
func LoadConfig(configFilePath string) (*viper.Viper, error) {
config, err := getConfig(configFilePath)
if err != nil {
return nil, err
}
configFile, err := GetConfigFilePath(config)
if err != nil {
return nil, err
}
// On Ubuntu OS, getConfig() function doesnt create a config file if its not present.
err = createConfigFile(configFile)
if err != nil {
return nil, err
}
// Acquire shared lock on the config.yaml.lock file.
// Retry it every second for 5 times if other goroutine is holding the lock i.e other cmd is writing to the config file.
// created a new file config.yaml.lock as windows os doesnt let us acuire lock on a file i.e config.yaml and write to it.
fileLock := flock.New(configFile + ".lock")
lockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = fileLock.TryRLockContext(lockCtx, 1*time.Second)
if err != nil {
return nil, fmt.Errorf("failed to acquire lock on '%s': %w", configFile, err)
}
defer func() {
err = fileLock.Unlock()
if err != nil {
output.LogInfo("failed to release lock on the config file : %s", configFile)
}
}()
err = config.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// It's ok the config file is not found, this could be the first time the CLI
// is running. Commands that require configuration will check for the data they need.
} else if os.IsNotExist(err) {
// It's ok the config file is not found, this could be the first time the CLI
// is running. Commands that require configuration will check for the data they need.
} else if err != nil {
return nil, err
}
return config, nil
}
// GetConfigFilePath attempts to find the user's config file path, first by checking if one has already been set, and if
// not, by using the user's home directory. If the home directory cannot be found, an error is returned.
func GetConfigFilePath(v *viper.Viper) (string, error) {
configFilePath := v.ConfigFileUsed()
// Set config file using the HOME directory.
if configFilePath == "" {
// This is extremely unlikely to fail on us. This would only happen
// if the user has no HOME (or USERPROFILE on Windows) directory.
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to find the user's home directory: %w", err)
}
configFilePath = path.Join(home, ".rad", "config.yaml")
}
return configFilePath, nil
}
// Required to be called while holding the exclusive lock on config.yaml.lock file.
//
// EditWorkspaces is a function that allows users to edit the workspaces in a config file. It takes in a context, config
// and an editor function as parameters. It reads the workspace section from the config, passes it to the editor function
// and then updates the workspace section in the config. It also checks for case-invariance to prevent duplicates. If an
// error occurs, it is returned to the caller.
func EditWorkspaces(ctx context.Context, config *viper.Viper, editor func(section *WorkspaceSection) error) error {
return SaveConfigOnLock(ctx, config, func(v *viper.Viper) error {
section, err := ReadWorkspaceSection(v)
if err != nil {
return err
}
err = editor(§ion)
if err != nil {
return err
}
// We need to check the workspaces for case-invariance. Viper stores everything as lowercase but it's
// possible for us to introduce bugs by creating duplicates. This section is only here so that we can easily identify a bug
// in the code that's calling EditWorkspaces.
names := map[string]bool{}
for name := range section.Items {
name = strings.ToLower(name)
_, ok := names[name]
if ok {
return fmt.Errorf("usage of name %q with different casings found. This is a bug in rad, the caller needs to lowercase the name before storage", name)
}
names[name] = true
}
UpdateWorkspaceSection(v, section)
return nil
})
}
// Save Config with exclusive lock on the config file
//
// SaveConfigOnLock acquires an exclusive lock on the config file and updates it with the given config, retrying every
// second for 5 times if another goroutine is holding the lock. It returns an error if it fails to acquire the lock or if
// the updateConfig function fails.
func SaveConfigOnLock(ctx context.Context, config *viper.Viper, updateConfig func(*viper.Viper) error) error {
// Acquire exclusive lock on the config.yaml.lock file.
// Retry it every second for 5 times if other goroutine is holding the lock i.e other cmd is writing to the config file.
configFilePath, err := GetConfigFilePath(config)
if err != nil {
return err
}
fileLock := flock.New(configFilePath + ".lock")
lockCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_, err = fileLock.TryLockContext(lockCtx, 1*time.Second)
if err != nil {
return fmt.Errorf("failed to acquire lock on '%s': %w", configFilePath, err)
}
defer func() {
err = fileLock.Unlock()
if err != nil {
output.LogInfo("failed to release lock on the config file : %s", configFilePath)
}
}()
err = updateConfig(config)
if err != nil {
return err
}
err = SaveConfig(config)
if err != nil {
return err
}
return nil
}
// SaveConfig saves the configuration to the file path specified in the viper instance. It returns an error if the file
// path is not found or if the configuration could not be written.
func SaveConfig(v *viper.Viper) error {
configFilePath, err := GetConfigFilePath(v)
if err != nil {
return err
}
err = v.WriteConfigAs(configFilePath)
if err != nil {
return fmt.Errorf("failed to write config to '%s': %w", configFilePath, err)
}
return nil
}
func validate(value any) error {
english := en.New()
uni := ut.New(english, english)
trans, _ := uni.GetTranslator("en")
val := validator.New()
err := en_translations.RegisterDefaultTranslations(val, trans)
if err != nil {
return err
}
err = val.Struct(value)
if err != nil {
return translateError(err, trans)
}
return nil
}
func translateError(err error, trans ut.Translator) error {
if err == nil {
return nil
}
messages := []string{}
for _, e := range err.(validator.ValidationErrors) {
translated := e.Translate(trans)
messages = append(messages, translated)
}
return errors.New(strings.Join(messages, ", "))
}