Skip to content

Fix profile existence check #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command {
if err != nil {
return fmt.Errorf("get profile: %w", err)
}
if activeProfile == "" {
activeProfile = "(no active profile, the default profile configuration will be used)"

profileExists, err := config.ProfileExists(activeProfile)
if err != nil {
return fmt.Errorf("check if profile exists: %w", err)
}
if !profileExists {
p.Warn("active profile does not exist, the default profile configuration will be used\n")
}
p.Debug(print.DebugLevel, "active configuration profile: %s", activeProfile)

Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const (
)

const (
configFolder = "stackit"
configFolder = "stackit"
defaultProfileName = "default"

configFileName = "cli-config"
configFileExtension = "json"
Expand Down Expand Up @@ -92,7 +93,7 @@ func InitConfig() {
cobra.CheckErr(err)

configFolderPath = defaultConfigFolderPath
if configProfile != "" {
if configProfile != defaultProfileName {
configFolderPath = filepath.Join(configFolderPath, profileRootFolder, configProfile) // If a profile is set, use the profile config folder
}

Expand Down
40 changes: 23 additions & 17 deletions internal/pkg/config/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ProfileEnvVar = "STACKIT_CLI_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 environment variable is not set and the profile file does not exist, it returns an empty string.
// 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) {
Expand All @@ -29,7 +29,7 @@ func GetProfile() (string, error) {
return "", fmt.Errorf("read profile from file: %w", err)
}
if !exists {
return "", nil
return defaultProfileName, nil
}
profile = contents
}
Expand All @@ -40,7 +40,7 @@ func GetProfile() (string, error) {
return "", fmt.Errorf("check if profile exists: %w", err)
}
if !profileExists {
return "", &errors.SetInexistentProfile{Profile: profile}
return defaultProfileName, nil
}

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

// Cannot create a profile with the default name
if profile == defaultProfileName {
return &errors.InvalidProfileNameError{
Profile: profile,
}
}

configFolderPath = filepath.Join(defaultConfigFolderPath, profileRootFolder, profile)

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

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)
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)
}
return fmt.Errorf("get active profile: %w", err)
}

p.Debug(print.DebugLevel, "current active profile: %q", currentProfile)

if !emptyProfile {
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 {
Expand Down Expand Up @@ -122,8 +128,8 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
// If the new profile already exists, it will be overwritten.
func DuplicateProfileConfiguration(p *print.Printer, currentProfile, newProfile string) error {
var currentConfigFilePath string
// If the current profile is empty, its the default profile
if currentProfile == "" {

if currentProfile == defaultProfileName {
currentConfigFilePath = filepath.Join(defaultConfigFolderPath, fmt.Sprintf("%s.%s", configFileName, configFileExtension))
} else {
currentConfigFilePath = filepath.Join(defaultConfigFolderPath, profileRootFolder, currentProfile, fmt.Sprintf("%s.%s", configFileName, configFileExtension))
Expand Down Expand Up @@ -154,7 +160,7 @@ func SetProfile(p *print.Printer, profile string) error {
}

if !profileExists {
return fmt.Errorf("profile %q does not exist", profile)
return &errors.SetInexistentProfile{Profile: profile}
}

err = os.WriteFile(profileFilePath, []byte(profile), os.ModePerm)
Expand Down
7 changes: 5 additions & 2 deletions internal/pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ Please double check if they are correctly configured.
For more details run:
$ stackit auth activate-service-account -h`

SET_INEXISTENT_PROFILE = `the configuration profile %[1]q does not exist.
SET_INEXISTENT_PROFILE = `the active configuration profile %[1]q does not exist.

To unset it, run:
$ stackit config profile unset

To create it, run:
$ stackit config profile create %[1]q`
Expand Down Expand Up @@ -122,7 +125,7 @@ For more details on the available storages for the configured flavor (%[3]s), ru

INVALID_PROFILE_NAME = `the profile name %q is invalid.

The profile name can only contain letters, numbers, and "-" and cannot be empty.`
The profile name can only contain letters, numbers, and "-" and cannot be empty or "default".`

USAGE_TIP = `For usage help, run:
$ %s --help`
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/fileutils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func WriteToFile(outputFileName, content string) (err error) {
}

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