summaryrefslogtreecommitdiff
path: root/internal/config
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-29 09:28:35 +0300
committerPaul Buetow <paul@buetow.org>2023-06-29 09:28:35 +0300
commit16602d176cb82da0f9980b95ac0ef6eec47953a2 (patch)
tree26ba82dc33a61b6de77e866d29cd719c31668ac1 /internal/config
parent84e5a4ae93eed6397a6cbf6091a0f5b3eacc78e2 (diff)
refactor config initialization
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go18
-rw-r--r--internal/config/config_test.go6
2 files changed, 8 insertions, 16 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index ec4ea67..2f8c62b 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -21,19 +21,7 @@ type Config struct {
nodeNumberCache map[string]int
}
-func New(arg any) (Config, error) {
- switch arg := arg.(type) {
- case string:
- return newFromConfigFile(arg)
- case Config:
- // Used to initialize a custom config from unit tests.
- return arg.initialize()
- default:
- panic("unable to initialize config")
- }
-}
-
-func newFromConfigFile(configFile string) (Config, error) {
+func New(configFile string) (Config, error) {
var conf Config
file, err := os.Open(configFile)
@@ -52,6 +40,10 @@ func newFromConfigFile(configFile string) (Config, error) {
return conf, err
}
+ return NewFromConfig(conf)
+}
+
+func NewFromConfig(conf Config) (Config, error) {
return conf.initialize()
}
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index af403ec..96d4998 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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
+ conf, _ := NewFromConfig(Config{Nodes: []string{"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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
+ conf, _ := NewFromConfig(Config{Nodes: []string{"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, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
+ conf, _ := NewFromConfig(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
lookupIP := func(addr string) ([]net.IP, error) {
switch addr {