diff --git a/internal/cmd/config/profile/delete/delete.go b/internal/cmd/config/profile/delete/delete.go index 7916d27b6..d468d93ed 100644 --- a/internal/cmd/config/profile/delete/delete.go +++ b/internal/cmd/config/profile/delete/delete.go @@ -51,6 +51,10 @@ func NewCmd(p *print.Printer) *cobra.Command { return &errors.DeleteInexistentProfile{Profile: model.Profile} } + if model.Profile == config.DefaultProfileName { + return &errors.DeleteDefaultProfile{DefaultProfile: config.DefaultProfileName} + } + if !model.AssumeYes { prompt := fmt.Sprintf("Are you sure you want to delete profile %q? (This cannot be undone)", model.Profile) err = p.PromptForConfirmation(prompt) diff --git a/internal/pkg/auth/storage.go b/internal/pkg/auth/storage.go index 20018a70e..d44cc1133 100644 --- a/internal/pkg/auth/storage.go +++ b/internal/pkg/auth/storage.go @@ -5,10 +5,12 @@ import ( "encoding/json" "errors" "fmt" + "os" "path/filepath" "github.com/stackitcloud/stackit-cli/internal/pkg/config" + pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/zalando/go-keyring" ) @@ -277,6 +279,10 @@ func DeleteProfileFromKeyring(profile string) error { return fmt.Errorf("validate profile: %w", err) } + if profile == config.DefaultProfileName { + return &pkgErrors.DeleteDefaultProfile{DefaultProfile: config.DefaultProfileName} + } + for _, key := range authFieldKeys { err := deleteAuthFieldInKeyring(profile, key) if err != nil { diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index 9f603e145..1591a024f 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -552,34 +552,20 @@ func TestDeleteProfileFromKeyring(t *testing.T) { activeProfile string isValid bool }{ + { - description: "base, default profile", - keys: authFieldKeys, - activeProfile: config.DefaultProfileName, - isValid: true, - }, - { - description: "missing keys, default profile", - keys: []authFieldKey{ - ACCESS_TOKEN, - SERVICE_ACCOUNT_EMAIL, - }, - activeProfile: config.DefaultProfileName, - isValid: true, - }, - { - description: "base, custom profile", + description: "base", keys: authFieldKeys, activeProfile: "test-profile", isValid: true, }, { - description: "missing keys, custom profile", + description: "missing keys", keys: []authFieldKey{ ACCESS_TOKEN, SERVICE_ACCOUNT_EMAIL, }, - activeProfile: config.DefaultProfileName, + activeProfile: "test-profile", isValid: true, }, { @@ -592,6 +578,11 @@ func TestDeleteProfileFromKeyring(t *testing.T) { keyringFails: true, isValid: false, }, + { + description: "default profile", + activeProfile: config.DefaultProfileName, + isValid: false, + }, } for _, tt := range tests { diff --git a/internal/pkg/config/profiles.go b/internal/pkg/config/profiles.go index bc05dc970..703031d27 100644 --- a/internal/pkg/config/profiles.go +++ b/internal/pkg/config/profiles.go @@ -275,7 +275,7 @@ func ListProfiles() ([]string, error) { } // DeleteProfile deletes a profile. -// If the profile does not exist, it returns an error. +// 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) @@ -283,6 +283,11 @@ func DeleteProfile(p *print.Printer, profile string) error { 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) diff --git a/internal/pkg/errors/errors.go b/internal/pkg/errors/errors.go index 17fc29ccf..68d281cdd 100644 --- a/internal/pkg/errors/errors.go +++ b/internal/pkg/errors/errors.go @@ -48,6 +48,8 @@ To create it, run: To list all profiles, run: $ stackit config profile list` + DELETE_DEFAULT_PROFILE = `the default configuration profile %q cannot be deleted.` + ARGUS_INVALID_INPUT_PLAN = `the instance plan was not correctly provided. Either provide the plan ID: @@ -173,6 +175,14 @@ func (e *DeleteInexistentProfile) Error() string { return fmt.Sprintf(DELETE_INEXISTENT_PROFILE, e.Profile) } +type DeleteDefaultProfile struct { + DefaultProfile string +} + +func (e *DeleteDefaultProfile) Error() string { + return fmt.Sprintf(DELETE_DEFAULT_PROFILE, e.DefaultProfile) +} + type ArgusInputPlanError struct { Cmd *cobra.Command Args []string