@@ -18,7 +18,7 @@ const ProfileEnvVar = "STACKIT_CLI_PROFILE"
18
18
// The profile is determined by the value of the STACKIT_CLI_PROFILE environment variable, or, if not set,
19
19
// by the contents of the profile file in the CLI config folder.
20
20
//
21
- // If the environment variable is not set and the profile file does not exist, it returns an empty string .
21
+ // 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 .
22
22
//
23
23
// If the profile is not valid, it returns an error.
24
24
func GetProfile () (string , error ) {
@@ -29,7 +29,7 @@ func GetProfile() (string, error) {
29
29
return "" , fmt .Errorf ("read profile from file: %w" , err )
30
30
}
31
31
if ! exists {
32
- return "" , nil
32
+ return defaultProfileName , nil
33
33
}
34
34
profile = contents
35
35
}
@@ -40,7 +40,7 @@ func GetProfile() (string, error) {
40
40
return "" , fmt .Errorf ("check if profile exists: %w" , err )
41
41
}
42
42
if ! profileExists {
43
- return "" , & errors. SetInexistentProfile { Profile : profile }
43
+ return defaultProfileName , nil
44
44
}
45
45
46
46
err = ValidateProfile (profile )
@@ -67,6 +67,13 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
67
67
return fmt .Errorf ("validate profile: %w" , err )
68
68
}
69
69
70
+ // Cannot create a profile with the default name
71
+ if profile == defaultProfileName {
72
+ return & errors.InvalidProfileNameError {
73
+ Profile : profile ,
74
+ }
75
+ }
76
+
70
77
configFolderPath = filepath .Join (defaultConfigFolderPath , profileRootFolder , profile )
71
78
72
79
// Error if the profile already exists
@@ -81,19 +88,18 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
81
88
}
82
89
p .Debug (print .DebugLevel , "created folder for the new profile: %s" , configFolderPath )
83
90
84
- currentProfile , err := GetProfile ()
85
- if err != nil {
86
- // Cleanup created directory
87
- cleanupErr := os .RemoveAll (configFolderPath )
88
- if cleanupErr != nil {
89
- return fmt .Errorf ("get active profile: %w, cleanup directories: %w" , err , cleanupErr )
91
+ if ! emptyProfile {
92
+ currentProfile , err := GetProfile ()
93
+ if err != nil {
94
+ // Cleanup created directory
95
+ cleanupErr := os .RemoveAll (configFolderPath )
96
+ if cleanupErr != nil {
97
+ return fmt .Errorf ("get active profile: %w, cleanup directories: %w" , err , cleanupErr )
98
+ }
99
+ return fmt .Errorf ("get active profile: %w" , err )
90
100
}
91
- return fmt .Errorf ("get active profile: %w" , err )
92
- }
93
-
94
- p .Debug (print .DebugLevel , "current active profile: %q" , currentProfile )
95
101
96
- if ! emptyProfile {
102
+ p . Debug ( print . DebugLevel , "current active profile: %q" , currentProfile )
97
103
p .Debug (print .DebugLevel , "duplicating profile configuration from %q to new profile %q" , currentProfile , profile )
98
104
err = DuplicateProfileConfiguration (p , currentProfile , profile )
99
105
if err != nil {
@@ -122,8 +128,8 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
122
128
// If the new profile already exists, it will be overwritten.
123
129
func DuplicateProfileConfiguration (p * print.Printer , currentProfile , newProfile string ) error {
124
130
var currentConfigFilePath string
125
- // If the current profile is empty, its the default profile
126
- if currentProfile == "" {
131
+
132
+ if currentProfile == defaultProfileName {
127
133
currentConfigFilePath = filepath .Join (defaultConfigFolderPath , fmt .Sprintf ("%s.%s" , configFileName , configFileExtension ))
128
134
} else {
129
135
currentConfigFilePath = filepath .Join (defaultConfigFolderPath , profileRootFolder , currentProfile , fmt .Sprintf ("%s.%s" , configFileName , configFileExtension ))
@@ -154,7 +160,7 @@ func SetProfile(p *print.Printer, profile string) error {
154
160
}
155
161
156
162
if ! profileExists {
157
- return fmt . Errorf ( "profile %q does not exist" , profile )
163
+ return & errors. SetInexistentProfile { Profile : profile }
158
164
}
159
165
160
166
err = os .WriteFile (profileFilePath , []byte (profile ), os .ModePerm )
0 commit comments