Skip to content

Commit 48f881b

Browse files
authored
fix profile existence check (#356)
1 parent 286c9c8 commit 48f881b

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

internal/cmd/root.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command {
6060
if err != nil {
6161
return fmt.Errorf("get profile: %w", err)
6262
}
63-
if activeProfile == "" {
64-
activeProfile = "(no active profile, the default profile configuration will be used)"
63+
64+
profileExists, err := config.ProfileExists(activeProfile)
65+
if err != nil {
66+
return fmt.Errorf("check if profile exists: %w", err)
67+
}
68+
if !profileExists {
69+
p.Warn("active profile does not exist, the default profile configuration will be used\n")
6570
}
6671
p.Debug(print.DebugLevel, "active configuration profile: %s", activeProfile)
6772

internal/pkg/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const (
4141
)
4242

4343
const (
44-
configFolder = "stackit"
44+
configFolder = "stackit"
45+
defaultProfileName = "default"
4546

4647
configFileName = "cli-config"
4748
configFileExtension = "json"
@@ -92,7 +93,7 @@ func InitConfig() {
9293
cobra.CheckErr(err)
9394

9495
configFolderPath = defaultConfigFolderPath
95-
if configProfile != "" {
96+
if configProfile != defaultProfileName {
9697
configFolderPath = filepath.Join(configFolderPath, profileRootFolder, configProfile) // If a profile is set, use the profile config folder
9798
}
9899

internal/pkg/config/profiles.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ProfileEnvVar = "STACKIT_CLI_PROFILE"
1818
// The profile is determined by the value of the STACKIT_CLI_PROFILE environment variable, or, if not set,
1919
// by the contents of the profile file in the CLI config folder.
2020
//
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.
2222
//
2323
// If the profile is not valid, it returns an error.
2424
func GetProfile() (string, error) {
@@ -29,7 +29,7 @@ func GetProfile() (string, error) {
2929
return "", fmt.Errorf("read profile from file: %w", err)
3030
}
3131
if !exists {
32-
return "", nil
32+
return defaultProfileName, nil
3333
}
3434
profile = contents
3535
}
@@ -40,7 +40,7 @@ func GetProfile() (string, error) {
4040
return "", fmt.Errorf("check if profile exists: %w", err)
4141
}
4242
if !profileExists {
43-
return "", &errors.SetInexistentProfile{Profile: profile}
43+
return defaultProfileName, nil
4444
}
4545

4646
err = ValidateProfile(profile)
@@ -67,6 +67,13 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
6767
return fmt.Errorf("validate profile: %w", err)
6868
}
6969

70+
// Cannot create a profile with the default name
71+
if profile == defaultProfileName {
72+
return &errors.InvalidProfileNameError{
73+
Profile: profile,
74+
}
75+
}
76+
7077
configFolderPath = filepath.Join(defaultConfigFolderPath, profileRootFolder, profile)
7178

7279
// Error if the profile already exists
@@ -81,19 +88,18 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
8188
}
8289
p.Debug(print.DebugLevel, "created folder for the new profile: %s", configFolderPath)
8390

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)
90100
}
91-
return fmt.Errorf("get active profile: %w", err)
92-
}
93-
94-
p.Debug(print.DebugLevel, "current active profile: %q", currentProfile)
95101

96-
if !emptyProfile {
102+
p.Debug(print.DebugLevel, "current active profile: %q", currentProfile)
97103
p.Debug(print.DebugLevel, "duplicating profile configuration from %q to new profile %q", currentProfile, profile)
98104
err = DuplicateProfileConfiguration(p, currentProfile, profile)
99105
if err != nil {
@@ -122,8 +128,8 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
122128
// If the new profile already exists, it will be overwritten.
123129
func DuplicateProfileConfiguration(p *print.Printer, currentProfile, newProfile string) error {
124130
var currentConfigFilePath string
125-
// If the current profile is empty, its the default profile
126-
if currentProfile == "" {
131+
132+
if currentProfile == defaultProfileName {
127133
currentConfigFilePath = filepath.Join(defaultConfigFolderPath, fmt.Sprintf("%s.%s", configFileName, configFileExtension))
128134
} else {
129135
currentConfigFilePath = filepath.Join(defaultConfigFolderPath, profileRootFolder, currentProfile, fmt.Sprintf("%s.%s", configFileName, configFileExtension))
@@ -154,7 +160,7 @@ func SetProfile(p *print.Printer, profile string) error {
154160
}
155161

156162
if !profileExists {
157-
return fmt.Errorf("profile %q does not exist", profile)
163+
return &errors.SetInexistentProfile{Profile: profile}
158164
}
159165

160166
err = os.WriteFile(profileFilePath, []byte(profile), os.ModePerm)

internal/pkg/errors/errors.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ Please double check if they are correctly configured.
3838
For more details run:
3939
$ stackit auth activate-service-account -h`
4040

41-
SET_INEXISTENT_PROFILE = `the configuration profile %[1]q does not exist.
41+
SET_INEXISTENT_PROFILE = `the active configuration profile %[1]q does not exist.
42+
43+
To unset it, run:
44+
$ stackit config profile unset
4245
4346
To create it, run:
4447
$ stackit config profile create %[1]q`
@@ -122,7 +125,7 @@ For more details on the available storages for the configured flavor (%[3]s), ru
122125

123126
INVALID_PROFILE_NAME = `the profile name %q is invalid.
124127
125-
The profile name can only contain letters, numbers, and "-" and cannot be empty.`
128+
The profile name can only contain letters, numbers, and "-" and cannot be empty or "default".`
126129

127130
USAGE_TIP = `For usage help, run:
128131
$ %s --help`

internal/pkg/fileutils/file_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func WriteToFile(outputFileName, content string) (err error) {
3030
}
3131

3232
// ReadFileIfExists reads the contents of a file and returns it as a string, along with a boolean indicating if the file exists.
33-
// If the file does not exist, it returns an empty string and no error.
33+
// If the file does not exist, it returns an empty string, false and no error.
3434
// If the file exists but cannot be read, it returns an error.
3535
func ReadFileIfExists(filePath string) (contents string, exists bool, err error) {
3636
_, err = os.Stat(filePath)

0 commit comments

Comments
 (0)