diff --git a/cmd/config/add_profile_test.go b/cmd/config/add_profile_test.go index 8280a73c..043d2b95 100644 --- a/cmd/config/add_profile_test.go +++ b/cmd/config/add_profile_test.go @@ -18,6 +18,21 @@ func TestConfigAddProfileCmd_Execute(t *testing.T) { testutils.CheckExpectedError(t, err, nil) } +// Test config add profile with multiple case-insensitive profile names +func TestConfigAddProfileCmd_CaseInsensitiveProfileNamesExecute(t *testing.T) { + err := testutils_cobra.ExecutePingcli(t, "config", "add-profile", + "--name", "same-profile", + "--description", "test description", + "--set-active=false") + testutils.CheckExpectedError(t, err, nil) + + err = testutils_cobra.ExecutePingcli(t, "config", "add-profile", + "--name", "SAME-PROFILE", + "--description", "test description", + "--set-active=false") + testutils.CheckExpectedError(t, err, nil) +} + // Test config add profile command fails when provided too many arguments func TestConfigAddProfileCmd_TooManyArgs(t *testing.T) { expectedErrorPattern := `^failed to execute 'pingcli config add-profile': command accepts 0 arg\(s\), received 1$` diff --git a/internal/profiles/koanf.go b/internal/profiles/koanf.go index d39a3644..682c5fd7 100644 --- a/internal/profiles/koanf.go +++ b/internal/profiles/koanf.go @@ -69,11 +69,8 @@ func KoanfValueFromOption(opt options.Option, pName string) (value string, ok bo ) // Case 1: Koanf Key is the ActiveProfile Key, get value from main koanf instance - if opt.KoanfKey != "" && strings.EqualFold(opt.KoanfKey, options.RootActiveProfileOption.KoanfKey) && mainKoanfInstance != nil { - kValue = mainKoanfInstance.KoanfInstance().Get("activeprofile") - if kValue == nil { - kValue = mainKoanfInstance.KoanfInstance().Get(opt.KoanfKey) - } + if opt.KoanfKey != "" && opt.KoanfKey == options.RootActiveProfileOption.KoanfKey && mainKoanfInstance != nil { + kValue = mainKoanfInstance.KoanfInstance().Get(opt.KoanfKey) } else { // // Case 2: --profile flag has been set, get value from set profile koanf instance // // Case 3: no --profile flag set, get value from active profile koanf instance defined in main koanf instance @@ -129,7 +126,7 @@ func (k KoanfConfig) ProfileNames() (profileNames []string) { mainKoanfKeys := k.KoanfInstance().All() for key := range mainKoanfKeys { // Do not add Active profile koanf key to profileNames - if strings.EqualFold(key, options.RootActiveProfileOption.KoanfKey) { + if key == options.RootActiveProfileOption.KoanfKey { continue } @@ -233,35 +230,6 @@ func (k KoanfConfig) GetProfileKoanf(pName string) (subKoanf *koanf.Koanf, err e } func (k KoanfConfig) WriteFile() (err error) { - // TODO - Remove this for loop prior to v0.7.0 release - for _, profileName := range k.ProfileNames() { - for key, val := range k.KoanfInstance().All() { - if profileName == key || !strings.Contains(key, profileName) { - continue - } - for _, opt := range options.Options() { - fullKoanfKeyValue := fmt.Sprintf("%s.%s", profileName, opt.KoanfKey) - if fullKoanfKeyValue == key { - continue - } - if strings.ToLower(fullKoanfKeyValue) == key { - err = k.KoanfInstance().Set(fullKoanfKeyValue, val) - if err != nil { - return fmt.Errorf("error setting koanf key %s: %w", fullKoanfKeyValue, err) - } - k.KoanfInstance().Delete(key) - } - } - } - } - - // TODO - Remove this originalActiveProfileKey logic prior to v0.7.0 release - // Delete the original active profile key if it exists and the new activeProfile exists - originalActiveProfileKey := strings.ToLower(options.RootActiveProfileOption.KoanfKey) - if k.KoanfInstance().Exists(originalActiveProfileKey) && k.KoanfInstance().Exists(options.RootActiveProfileOption.KoanfKey) { - k.KoanfInstance().Delete(strings.ToLower(originalActiveProfileKey)) - } - encodedConfig, err := k.KoanfInstance().Marshal(yaml.Parser()) if err != nil { return fmt.Errorf("error marshalling koanf: %w", err) @@ -323,7 +291,7 @@ func (k KoanfConfig) DefaultMissingKoanfKeys() (err error) { } for _, opt := range options.Options() { - if opt.KoanfKey == "" || strings.EqualFold(opt.KoanfKey, options.RootActiveProfileOption.KoanfKey) { + if opt.KoanfKey == "" || opt.KoanfKey == options.RootActiveProfileOption.KoanfKey { continue }