diff options
| author | Paul Buetow <paul@buetow.org> | 2023-07-02 20:10:38 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-07-02 20:10:38 +0300 |
| commit | 9e099d15bd31f80751b9b2ac38c31bf130528329 (patch) | |
| tree | 118d42839d53a237ea64658229ab9c9762c4f35a /internal/config | |
| parent | 5978480b49c152e458055bb3a6b1b4ba9afa54e9 (diff) | |
refactor config to use functional options pattern
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 46 | ||||
| -rw-r--r-- | internal/config/config_test.go | 6 |
2 files changed, 44 insertions, 8 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index cb144f3..988315a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,7 +24,7 @@ type Config struct { SMTPServer string `json:"SMTPServer,omitempty"` } -func New(configFile string) (Config, error) { +func NewFromConfigFile(configFile string) (Config, error) { var conf Config file, err := os.Open(configFile) @@ -43,14 +43,50 @@ func New(configFile string) (Config, error) { return conf, err } - return NewFromConfig(conf) + return conf.setDefaults() } -func NewFromConfig(conf Config) (Config, error) { - return conf.initialize() +func New(options ...func(*Config)) (Config, error) { + var conf Config + + for _, opt := range options { + opt(&conf) + } + + return conf.setDefaults() +} + +func WithStateDir(stateDir string) func(*Config) { + return func(conf *Config) { + conf.StateDir = stateDir + } +} + +func WithAddress(address string) func(*Config) { + return func(conf *Config) { + conf.Address = address + } +} + +func WithNodes(nodes ...string) func(*Config) { + return func(conf *Config) { + conf.Nodes = append(conf.Nodes, nodes...) + } +} + +func WithMyID(id string) func(*Config) { + return func(conf *Config) { + conf.MyID = id + } +} + +func WithRelaxedMode() func(*Config) { + return func(conf *Config) { + conf.RelaxedMode = true + } } -func (conf Config) initialize() (Config, error) { +func (conf Config) setDefaults() (Config, error) { if conf.LoopIntervalS == 0 { conf.LoopIntervalS = 10 } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 96d4998..e24c048 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -8,7 +8,7 @@ import ( func TestNodeNumber(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) num := conf.NodeNumber("localhost") if num != 0 { @@ -23,7 +23,7 @@ func TestNodeNumber(t *testing.T) { func TestIsNode(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) remoteAddr := "localhost:323232" if !conf.IsNode(remoteAddr) { @@ -38,7 +38,7 @@ func TestIsNode(t *testing.T) { func TestIsNodeWithLookup(t *testing.T) { t.Parallel() - conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}) + conf, _ := New(WithNodes("localhost:1234", "hamburger:4321")) lookupIP := func(addr string) ([]net.IP, error) { switch addr { |
