diff --git a/internal/config/config_type.go b/internal/config/config_type.go index 1a179c41d..7842f10b6 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -10,14 +10,14 @@ import ( "gopkg.in/yaml.v3" ) -type ConfigOption struct { +type configOption struct { Key string Description string DefaultValue string AllowedValues []string } -var configOptions = []ConfigOption{ +var configOptions = []configOption{ { Key: "git_protocol", Description: "the protocol to use for git clone and push operations", @@ -42,13 +42,45 @@ var configOptions = []ConfigOption{ }, } -func ConfigOptions() []ConfigOption { +func ConfigOptions() []configOption { return configOptions } -var configValues = map[string][]string{ - "git_protocol": {"ssh", "https"}, - "prompt": {"enabled", "disabled"}, +func ValidateKey(key string) error { + for _, configKey := range configOptions { + if key == configKey.Key { + return nil + } + } + + return fmt.Errorf("invalid key") +} + +func ValidateValue(key, value string) error { + var validValues []string + + for _, v := range configOptions { + if v.Key == key { + validValues = v.AllowedValues + break + } + } + + if validValues == nil { + return nil + } + + for _, v := range validValues { + if v == value { + return nil + } + } + + return &InvalidValueError{ValidValues: validValues} +} + +func (e InvalidValueError) Error() string { + return "invalid value" } // This interface describes interacting with some persistent configuration for gh. @@ -310,29 +342,7 @@ type InvalidValueError struct { ValidValues []string } -func (e InvalidValueError) Error() string { - return "invalid value" -} - -func validateConfigEntry(key, value string) error { - validValues, found := configValues[key] - if !found { - return nil - } - - for _, v := range validValues { - if v == value { - return nil - } - } - - return &InvalidValueError{ValidValues: validValues} -} - func (c *fileConfig) Set(hostname, key, value string) error { - if err := validateConfigEntry(key, value); err != nil { - return err - } if hostname == "" { return c.SetStringValue(key, value) } else { diff --git a/internal/config/config_type_test.go b/internal/config/config_type_test.go index 46b9016f4..47295230f 100644 --- a/internal/config/config_type_test.go +++ b/internal/config/config_type_test.go @@ -28,7 +28,6 @@ func Test_fileConfig_Set(t *testing.T) { example.com: editor: vim `, hostsBuf.String()) - assert.EqualError(t, c.Set("github.com", "git_protocol", "sshpps"), "invalid value") } func Test_defaultConfig(t *testing.T) { @@ -70,16 +69,33 @@ func Test_defaultConfig(t *testing.T) { assert.Equal(t, expansion, "pr checkout") } -func Test_validateConfigEntry(t *testing.T) { - err := validateConfigEntry("git_protocol", "sshpps") +func Test_ValidateValue(t *testing.T) { + err := ValidateValue("git_protocol", "sshpps") assert.EqualError(t, err, "invalid value") - err = validateConfigEntry("git_protocol", "ssh") + err = ValidateValue("git_protocol", "ssh") assert.Nil(t, err) - err = validateConfigEntry("editor", "vim") + err = ValidateValue("editor", "vim") assert.Nil(t, err) - err = validateConfigEntry("got", "123") + err = ValidateValue("got", "123") assert.Nil(t, err) } + +func Test_ValidateKey(t *testing.T) { + err := ValidateKey("invalid") + assert.EqualError(t, err, "invalid key") + + err = ValidateKey("git_protocol") + assert.NoError(t, err) + + err = ValidateKey("editor") + assert.NoError(t, err) + + err = ValidateKey("prompt") + assert.NoError(t, err) + + err = ValidateKey("pager") + assert.NoError(t, err) +}