Skip to content

Commit 52d2eb7

Browse files
CDI-580: Support Case-Insensitive Keys in 'Config' Commands (#121)
- Update 'set' and 'unset' subcommands to change actual keys, not the user provided case-insensitive keys.
1 parent 4fed68c commit 52d2eb7

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

internal/commands/config/set_internal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func RunInternalConfigSet(kvPair string) (err error) {
3939
return fmt.Errorf("failed to set configuration: %w", err)
4040
}
4141

42-
if err = setValue(subKoanf, vKey, vValue, opt.Type); err != nil {
42+
if err = setValue(subKoanf, opt.KoanfKey, vValue, opt.Type); err != nil {
4343
return fmt.Errorf("failed to set configuration: %w", err)
4444
}
4545

@@ -60,9 +60,9 @@ func RunInternalConfigSet(kvPair string) (err error) {
6060
}
6161

6262
if opt.Sensitive && strings.EqualFold(unmaskOptionVal, "false") {
63-
msgStr += fmt.Sprintf("%s=%s", vKey, profiles.MaskValue(vVal))
63+
msgStr += fmt.Sprintf("%s=%s", opt.KoanfKey, profiles.MaskValue(vVal))
6464
} else {
65-
msgStr += fmt.Sprintf("%s=%s", vKey, vVal)
65+
msgStr += fmt.Sprintf("%s=%s", opt.KoanfKey, vVal)
6666
}
6767

6868
output.Success(msgStr, nil)

internal/commands/config/set_internal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/pingidentity/pingcli/internal/configuration/options"
99
"github.com/pingidentity/pingcli/internal/customtypes"
10+
"github.com/pingidentity/pingcli/internal/profiles"
1011
"github.com/pingidentity/pingcli/internal/testing/testutils"
1112
"github.com/pingidentity/pingcli/internal/testing/testutils_koanf"
1213
)
@@ -129,4 +130,14 @@ func Test_RunInternalConfigSet_CaseInsensitiveKeys(t *testing.T) {
129130
if err != nil {
130131
t.Errorf("RunInternalConfigSet returned error: %v", err)
131132
}
133+
134+
// Make sure the actual correct key was set, not the case-insensitive one
135+
vVal, err := profiles.GetOptionValue(options.RootColorOption)
136+
if err != nil {
137+
t.Errorf("GetOptionValue returned error: %v", err)
138+
}
139+
140+
if vVal != "true" {
141+
t.Errorf("Expected %s to be true, got %v", options.RootColorOption.KoanfKey, vVal)
142+
}
132143
}

internal/commands/config/unset_internal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func RunInternalConfigUnset(koanfKey string) (err error) {
3232
return fmt.Errorf("failed to unset configuration: %w", err)
3333
}
3434

35-
err = subKoanf.Set(koanfKey, opt.DefaultValue)
35+
err = subKoanf.Set(opt.KoanfKey, opt.DefaultValue)
3636
if err != nil {
3737
return fmt.Errorf("failed to unset configuration: %w", err)
3838
}
@@ -54,9 +54,9 @@ func RunInternalConfigUnset(koanfKey string) (err error) {
5454
}
5555

5656
if opt.Sensitive && strings.EqualFold(unmaskOptionVal, "false") {
57-
msgStr += fmt.Sprintf("%s=%s", koanfKey, profiles.MaskValue(vVal))
57+
msgStr += fmt.Sprintf("%s=%s", opt.KoanfKey, profiles.MaskValue(vVal))
5858
} else {
59-
msgStr += fmt.Sprintf("%s=%s", koanfKey, vVal)
59+
msgStr += fmt.Sprintf("%s=%s", opt.KoanfKey, vVal)
6060
}
6161

6262
output.Success(msgStr, nil)

internal/commands/config/unset_internal_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/pingidentity/pingcli/internal/configuration/options"
99
"github.com/pingidentity/pingcli/internal/customtypes"
10+
"github.com/pingidentity/pingcli/internal/profiles"
1011
"github.com/pingidentity/pingcli/internal/testing/testutils"
1112
"github.com/pingidentity/pingcli/internal/testing/testutils_koanf"
1213
)
@@ -62,3 +63,23 @@ func Test_RunInternalConfigUnset_InvalidProfileName(t *testing.T) {
6263
err := RunInternalConfigUnset("noColor")
6364
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
6465
}
66+
67+
// Test RunInternalConfigUnset function succeeds with case-insensitive key
68+
func Test_RunInternalConfigUnset_CaseInsensitiveKeys(t *testing.T) {
69+
testutils_koanf.InitKoanfs(t)
70+
71+
err := RunInternalConfigUnset("NoCoLoR")
72+
if err != nil {
73+
t.Errorf("RunInternalConfigUnset returned error: %v", err)
74+
}
75+
76+
// Make sure the actual correct key was unset, not the case-insensitive one
77+
vVal, err := profiles.GetOptionValue(options.RootColorOption)
78+
if err != nil {
79+
t.Errorf("GetOptionValue returned error: %v", err)
80+
}
81+
82+
if vVal != options.RootColorOption.DefaultValue.String() {
83+
t.Errorf("Expected %s to be %s, got %v", options.RootColorOption.KoanfKey, options.RootColorOption.DefaultValue.String(), vVal)
84+
}
85+
}

0 commit comments

Comments
 (0)