@@ -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-
332345func (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 {
0 commit comments