-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathprofiles.go
More file actions
454 lines (387 loc) · 13.7 KB
/
profiles.go
File metadata and controls
454 lines (387 loc) · 13.7 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package config
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
)
const ProfileEnvVar = "STACKIT_CLI_PROFILE"
// GetProfile returns the current profile to be used by the CLI.
// The profile is determined by the value of the STACKIT_CLI_PROFILE environment variable, or, if not set,
// by the contents of the profile file in the CLI config folder.
// If the profile is not set (env var or profile file) or is set but does not exist, it falls back to the default profile.
// If the profile is not valid, it returns an error.
func GetProfile() (string, error) {
_, profile, _, err := GetConfiguredProfile()
if err != nil {
return "", err
}
return profile, nil
}
// GetConfiguredProfile returns the profile configured by the user, the profile to be used by the CLI and the method used to configure the profile.
// The profile is determined by the value of the STACKIT_CLI_PROFILE environment variable, or, if not set,
// by the contents of the profile file in the CLI config folder.
// If the configured profile is not set (env var or profile file) or is set but does not exist, it falls back to the default profile.
// The configuration method can be environment variable, profile file or empty if profile is not configured.
// If the profile is not valid, it returns an error.
func GetConfiguredProfile() (configuredProfile, activeProfile, configurationMethod string, err error) {
var configMethod string
profile, profileSetInEnv := GetProfileFromEnv()
if !profileSetInEnv {
contents, exists, err := fileutils.ReadFileIfExists(profileFilePath)
if err != nil {
return "", "", "", fmt.Errorf("read profile from file: %w", err)
}
if !exists {
// No profile set in env or file
return DefaultProfileName, DefaultProfileName, "", nil
}
profile = contents
configMethod = "profile file"
} else {
configMethod = "environment variable"
}
// Make sure the profile exists
profileExists, err := ProfileExists(profile)
if err != nil {
return "", "", "", fmt.Errorf("check if profile exists: %w", err)
}
if !profileExists {
// Profile is configured but does not exist
return profile, DefaultProfileName, configMethod, nil
}
err = ValidateProfile(profile)
if err != nil {
return "", "", "", fmt.Errorf("validate profile: %w", err)
}
return profile, profile, configMethod, nil
}
// GetProfileFromEnv returns the profile from the environment variable.
// If the environment variable is not set, it returns an empty string.
// If the profile is not valid, it returns an error.
func GetProfileFromEnv() (string, bool) {
return os.LookupEnv(ProfileEnvVar)
}
// CreateProfile creates a new profile.
// If emptyProfile is true, it creates an empty profile. Otherwise, copies the config from the current profile to the new profile.
// If setProfile is true, it sets the new profile as the active profile.
// If the profile already exists and ignoreExisting is false, it returns an error.
func CreateProfile(p *print.Printer, profile string, setProfile, ignoreExisting, emptyProfile bool) error {
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
}
// Cannot create a profile with the default name
if profile == DefaultProfileName {
return &errors.InvalidProfileNameError{
Profile: profile,
}
}
configFolderPath = GetProfileFolderPath(profile)
// Error if the profile already exists
_, err = os.Stat(configFolderPath)
if err == nil {
if ignoreExisting {
if setProfile {
err = SetProfile(p, profile)
if err != nil {
return fmt.Errorf("set profile: %w", err)
}
}
return nil
}
return fmt.Errorf("profile %q already exists", profile)
}
err = os.MkdirAll(configFolderPath, 0o750)
if err != nil {
return fmt.Errorf("create config folder: %w", err)
}
p.Debug(print.DebugLevel, "created folder for the new profile: %s", configFolderPath)
if !emptyProfile {
currentProfile, err := GetProfile()
if err != nil {
// Cleanup created directory
cleanupErr := os.RemoveAll(configFolderPath)
if cleanupErr != nil {
return fmt.Errorf("get active profile: %w, cleanup directories: %w", err, cleanupErr)
}
return fmt.Errorf("get active profile: %w", err)
}
p.Debug(print.DebugLevel, "current active profile: %q", currentProfile)
p.Debug(print.DebugLevel, "duplicating profile configuration from %q to new profile %q", currentProfile, profile)
err = DuplicateProfileConfiguration(p, currentProfile, profile)
if err != nil {
// Cleanup created directory
cleanupErr := os.RemoveAll(configFolderPath)
if cleanupErr != nil {
return fmt.Errorf("get active profile: %w, cleanup directories: %w", err, cleanupErr)
}
return fmt.Errorf("duplicate profile configuration: %w", err)
}
}
if setProfile {
err = SetProfile(p, profile)
if err != nil {
return fmt.Errorf("set profile: %w", err)
}
}
return nil
}
// DuplicateProfileConfiguration duplicates the current profile configuration to a new profile.
// It copies the config file from the current profile to the new profile.
// If the current profile does not exist, it does nothing.
// If the new profile already exists, it will be overwritten.
func DuplicateProfileConfiguration(p *print.Printer, currentProfile, newProfile string) error {
currentProfileFolder := GetProfileFolderPath(currentProfile)
currentConfigFilePath := getConfigFilePath(currentProfileFolder)
newConfigFilePath := getConfigFilePath(configFolderPath)
// If the source profile configuration does not exist, do nothing
_, err := os.Stat(currentConfigFilePath)
if err != nil {
if os.IsNotExist(err) {
p.Debug(print.DebugLevel, "current profile %q has no configuration, nothing to duplicate", currentProfile)
return nil
}
return fmt.Errorf("get current profile configuration: %w", err)
}
err = fileutils.CopyFile(currentConfigFilePath, newConfigFilePath)
if err != nil {
return fmt.Errorf("copy config file: %w", err)
}
p.Debug(print.DebugLevel, "created new configuration for profile %q based on %q in: %s", newProfile, currentProfile, newConfigFilePath)
return nil
}
// SetProfile sets the profile to be used by the CLI.
func SetProfile(p *print.Printer, profile string) error {
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
}
profileExists, err := ProfileExists(profile)
if err != nil {
return fmt.Errorf("check if profile exists: %w", err)
}
if !profileExists {
return &errors.SetInexistentProfile{Profile: profile}
}
if profileFilePath == "" {
profileFilePath = getInitialProfileFilePath()
}
err = os.WriteFile(profileFilePath, []byte(profile), 0o600)
if err != nil {
return fmt.Errorf("write profile to file: %w", err)
}
p.Debug(print.DebugLevel, "persisted new active profile in: %s", profileFilePath)
configFolderPath = GetProfileFolderPath(profile)
p.Debug(print.DebugLevel, "profile %q is now active", profile)
return nil
}
// UnsetProfile removes the profile file.
// If the profile file does not exist, it does nothing.
func UnsetProfile(p *print.Printer) error {
err := os.Remove(profileFilePath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove profile file: %w", err)
}
if p != nil {
p.Debug(print.DebugLevel, "removed active profile file: %s", profileFilePath)
}
return nil
}
// ValidateProfile validates the profile name.
// It can only use lowercase letters, numbers, or "-" and cannot be empty.
// It can't start with a "-".
// If the profile is invalid, it returns an error.
func ValidateProfile(profile string) error {
match, err := regexp.MatchString("^[a-z0-9][a-z0-9-]+$", profile)
if err != nil {
return fmt.Errorf("match string regex: %w", err)
}
if !match {
return &errors.InvalidProfileNameError{
Profile: profile,
}
}
return nil
}
func ProfileExists(profile string) (bool, error) {
_, err := os.Stat(GetProfileFolderPath(profile))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("get profile folder: %w", err)
}
return true, nil
}
// GetProfileFolderPath returns the path to the folder where the profile configuration is stored.
// If the profile is the default profile, it returns the default config folder path.
func GetProfileFolderPath(profile string) string {
if defaultConfigFolderPath == "" {
defaultConfigFolderPath = getInitialConfigDir()
}
if profile == DefaultProfileName {
return defaultConfigFolderPath
}
return filepath.Join(defaultConfigFolderPath, profileRootFolder, profile)
}
// ListProfiles returns a list of all non-default profiles.
// If there are no profiles, it returns an empty list.
func ListProfiles() ([]string, error) {
profiles := []string{}
// Check if the profile root folder exists
_, err := os.Stat(filepath.Join(defaultConfigFolderPath, profileRootFolder))
if err != nil {
if os.IsNotExist(err) {
return profiles, nil
}
return nil, fmt.Errorf("get profile root folder: %w", err)
}
profileFolders, err := os.ReadDir(filepath.Join(defaultConfigFolderPath, profileRootFolder))
if err != nil {
return nil, fmt.Errorf("read profile folders: %w", err)
}
for _, profileFolder := range profileFolders {
if profileFolder.IsDir() {
profiles = append(profiles, profileFolder.Name())
}
}
return profiles, nil
}
// DeleteProfile deletes a profile.
// If the profile does not exist or is the default profile, it returns an error.
// If the profile is the active profile, it sets the active profile to the default profile.
func DeleteProfile(p *print.Printer, profile string) error {
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
}
// Default profile cannot be deleted
if profile == DefaultProfileName {
return &errors.DeleteDefaultProfile{DefaultProfile: DefaultProfileName}
}
activeProfile, err := GetProfile()
if err != nil {
return fmt.Errorf("get active profile: %w", err)
}
profileExists, err := ProfileExists(profile)
if err != nil {
return fmt.Errorf("check if profile exists: %w", err)
}
if !profileExists {
return &errors.DeleteInexistentProfile{Profile: profile}
}
err = os.RemoveAll(filepath.Join(defaultConfigFolderPath, profileRootFolder, profile))
if err != nil {
return fmt.Errorf("remove profile folder: %w", err)
}
if activeProfile == profile {
err = UnsetProfile(p)
if err != nil {
return fmt.Errorf("unset profile: %w", err)
}
}
if p != nil {
p.Debug(print.DebugLevel, "deleted profile %q", profile)
}
return nil
}
// ImportProfile imports a profile configuration
// It imports the profile with the name profileName and a config json.
// If setAsActive is true, it set the new profile as the active profile.
func ImportProfile(p *print.Printer, profileName, config string, setAsActive bool) error {
err := ValidateProfile(profileName)
if err != nil || profileName == DefaultProfileName {
return &errors.InvalidProfileNameError{Profile: profileName}
}
exists, err := ProfileExists(profileName)
if err != nil {
return fmt.Errorf("check if profile exists: %w", err)
}
if exists {
return &errors.ProfileAlreadyExistsError{Profile: profileName}
}
importConfig := &map[string]interface{}{}
err = json.Unmarshal([]byte(config), importConfig)
if err != nil {
return fmt.Errorf("unmarshal config: %w", err)
}
configFolderPath = GetProfileFolderPath(profileName)
err = os.MkdirAll(configFolderPath, 0o750)
if err != nil {
return fmt.Errorf("create config folder: %w", err)
}
content, err := json.MarshalIndent(importConfig, "", " ")
if err != nil {
cleanupErr := os.RemoveAll(configFolderPath)
if cleanupErr != nil {
return fmt.Errorf("json marshal config: %w, cleanup directories: %w", err, cleanupErr)
}
return fmt.Errorf("marshal config file: %w", err)
}
filePath := getConfigFilePath(configFolderPath)
err = os.WriteFile(filePath, content, 0o600)
if err != nil {
cleanupErr := os.RemoveAll(configFolderPath)
if cleanupErr != nil {
return fmt.Errorf("write config file: %w, cleanup directories: %w", err, cleanupErr)
}
return fmt.Errorf("write config file: %w", err)
}
if p.IsVerbosityDebug() {
p.Debug(print.DebugLevel, "profile %q imported", profileName)
}
if setAsActive {
err := SetProfile(&print.Printer{}, profileName)
if err != nil {
return fmt.Errorf("set active profile: %w", err)
}
}
if p.IsVerbosityDebug() {
p.Debug(print.DebugLevel, "active profile %q is now active", profileName)
}
return nil
}
// ExportProfile exports a profile configuration
// Is exports the profile to the exportPath. The exportPath must contain the filename.
func ExportProfile(p *print.Printer, profile, exportPath string) error {
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile name: %w", err)
}
exists, err := ProfileExists(profile)
if err != nil {
return fmt.Errorf("check if profile exists: %w", err)
}
if !exists {
return &errors.ProfileDoesNotExistError{Profile: profile}
}
profilePath := GetProfileFolderPath(profile)
configFile := getConfigFilePath(profilePath)
stats, err := os.Stat(exportPath)
if err == nil {
if stats.IsDir() {
return fmt.Errorf("export path %q is a directory. Please specify a full path", exportPath)
}
return &errors.FileAlreadyExistsError{Filename: exportPath}
}
_, err = os.Stat(configFile)
if os.IsNotExist(err) {
// viper.SafeWriteConfigAs would not overwrite the target, so we use WriteConfigAs for the same behavior as CopyFile
err = viper.WriteConfigAs(exportPath)
} else {
err = fileutils.CopyFile(configFile, exportPath)
}
if err != nil {
return fmt.Errorf("export config file to %q: %w", exportPath, err)
}
if p != nil {
p.Debug(print.DebugLevel, "exported profile %q to %q", profile, exportPath)
}
return nil
}