github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/mediators/config/registry.go (about) 1 package config 2 3 type Type int 4 5 const ( 6 String Type = iota 7 Int 8 Bool 9 ) 10 11 // Event is run when a user tries to set or get a config value via `state config` 12 type Event func(value interface{}) (interface{}, error) 13 14 var EmptyEvent = func(value interface{}) (interface{}, error) { return value, nil } 15 16 // Option defines what a config value's name and type should be, along with any get/set events 17 type Option struct { 18 Name string 19 Type Type 20 Default interface{} 21 GetEvent Event 22 SetEvent Event 23 isRegistered bool 24 } 25 26 type Registry map[string]Option 27 28 var registry = make(Registry) 29 30 // GetOption returns a config option, regardless of whether or not it has been registered. 31 // Use KnownOption to determine if the returned option has been previously registered. 32 func GetOption(key string) Option { 33 rule, ok := registry[key] 34 if !ok { 35 return Option{key, String, "", EmptyEvent, EmptyEvent, false} 36 } 37 return rule 38 } 39 40 // Registers a config option without get/set events. 41 func RegisterOption(key string, t Type, defaultValue interface{}) { 42 RegisterOptionWithEvents(key, t, defaultValue, EmptyEvent, EmptyEvent) 43 } 44 45 // Registers a config option with get/set events. 46 func RegisterOptionWithEvents(key string, t Type, defaultValue interface{}, get, set Event) { 47 registry[key] = Option{key, t, defaultValue, get, set, true} 48 } 49 50 func KnownOption(rule Option) bool { 51 return rule.isRegistered 52 }