X Tutup
Skip to content

Commit e6be06f

Browse files
committed
Cleanup duplicate config options
1 parent e3fbe47 commit e6be06f

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

internal/config/config_type.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"gopkg.in/yaml.v3"
1111
)
1212

13-
type ConfigOption struct {
13+
type configOption struct {
1414
Key string
1515
Description string
1616
DefaultValue string
1717
AllowedValues []string
1818
}
1919

20-
var configOptions = []ConfigOption{
20+
var configOptions = []configOption{
2121
{
2222
Key: "git_protocol",
2323
Description: "the protocol to use for git clone and push operations",
@@ -42,13 +42,45 @@ var configOptions = []ConfigOption{
4242
},
4343
}
4444

45-
func ConfigOptions() []ConfigOption {
45+
func ConfigOptions() []configOption {
4646
return configOptions
4747
}
4848

49-
var configValues = map[string][]string{
50-
"git_protocol": {"ssh", "https"},
51-
"prompt": {"enabled", "disabled"},
49+
func ValidateKey(key string) error {
50+
for _, configKey := range configOptions {
51+
if key == configKey.Key {
52+
return nil
53+
}
54+
}
55+
56+
return fmt.Errorf("invalid key")
57+
}
58+
59+
func ValidateValue(key, value string) error {
60+
var validValues []string
61+
62+
for _, v := range configOptions {
63+
if v.Key == key {
64+
validValues = v.AllowedValues
65+
break
66+
}
67+
}
68+
69+
if validValues == nil {
70+
return nil
71+
}
72+
73+
for _, v := range validValues {
74+
if v == value {
75+
return nil
76+
}
77+
}
78+
79+
return &InvalidValueError{ValidValues: validValues}
80+
}
81+
82+
func (e InvalidValueError) Error() string {
83+
return "invalid value"
5284
}
5385

5486
// This interface describes interacting with some persistent configuration for gh.
@@ -310,29 +342,7 @@ type InvalidValueError struct {
310342
ValidValues []string
311343
}
312344

313-
func (e InvalidValueError) Error() string {
314-
return "invalid value"
315-
}
316-
317-
func validateConfigEntry(key, value string) error {
318-
validValues, found := configValues[key]
319-
if !found {
320-
return nil
321-
}
322-
323-
for _, v := range validValues {
324-
if v == value {
325-
return nil
326-
}
327-
}
328-
329-
return &InvalidValueError{ValidValues: validValues}
330-
}
331-
332345
func (c *fileConfig) Set(hostname, key, value string) error {
333-
if err := validateConfigEntry(key, value); err != nil {
334-
return err
335-
}
336346
if hostname == "" {
337347
return c.SetStringValue(key, value)
338348
} else {

internal/config/config_type_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func Test_fileConfig_Set(t *testing.T) {
2828
example.com:
2929
editor: vim
3030
`, hostsBuf.String())
31-
assert.EqualError(t, c.Set("github.com", "git_protocol", "sshpps"), "invalid value")
3231
}
3332

3433
func Test_defaultConfig(t *testing.T) {
@@ -70,16 +69,33 @@ func Test_defaultConfig(t *testing.T) {
7069
assert.Equal(t, expansion, "pr checkout")
7170
}
7271

73-
func Test_validateConfigEntry(t *testing.T) {
74-
err := validateConfigEntry("git_protocol", "sshpps")
72+
func Test_ValidateValue(t *testing.T) {
73+
err := ValidateValue("git_protocol", "sshpps")
7574
assert.EqualError(t, err, "invalid value")
7675

77-
err = validateConfigEntry("git_protocol", "ssh")
76+
err = ValidateValue("git_protocol", "ssh")
7877
assert.Nil(t, err)
7978

80-
err = validateConfigEntry("editor", "vim")
79+
err = ValidateValue("editor", "vim")
8180
assert.Nil(t, err)
8281

83-
err = validateConfigEntry("got", "123")
82+
err = ValidateValue("got", "123")
8483
assert.Nil(t, err)
8584
}
85+
86+
func Test_ValidateKey(t *testing.T) {
87+
err := ValidateKey("invalid")
88+
assert.EqualError(t, err, "invalid key")
89+
90+
err = ValidateKey("git_protocol")
91+
assert.NoError(t, err)
92+
93+
err = ValidateKey("editor")
94+
assert.NoError(t, err)
95+
96+
err = ValidateKey("prompt")
97+
assert.NoError(t, err)
98+
99+
err = ValidateKey("pager")
100+
assert.NoError(t, err)
101+
}

0 commit comments

Comments
 (0)
X Tutup