Skip to content
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
15 changes: 15 additions & 0 deletions cmd/config/add_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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$`
Expand Down
40 changes: 4 additions & 36 deletions internal/profiles/koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down