summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/config/client/client.go12
-rw-r--r--internal/config/config_test.go38
-rw-r--r--internal/config/enver.go24
-rw-r--r--internal/config/server/server.go20
4 files changed, 47 insertions, 47 deletions
diff --git a/internal/config/client/client.go b/internal/config/client/client.go
index ada1a76..94f0d98 100644
--- a/internal/config/client/client.go
+++ b/internal/config/client/client.go
@@ -26,16 +26,16 @@ func New(configFile string) (ClientConfig, error) {
log.Println("Skipping config file:", err)
}
- conf.Servers = config.Env[config.ToStringSlice]("GOS_SERVERS", conf.Servers)
- conf.APIKey = config.Env[config.ToString]("GOS_API_KEY", conf.APIKey)
- conf.Editor = config.Env[config.ToString]("GOS_EDITOR", "EDITOR", conf.Editor, "vi")
+ conf.Servers = config.Env[config.StrSlice]("GOS_SERVERS", conf.Servers)
+ conf.APIKey = config.Env[config.Str]("GOS_API_KEY", conf.APIKey)
+ conf.Editor = config.Env[config.Str]("GOS_EDITOR", "EDITOR", conf.Editor, "vi")
defaultDataDir := fmt.Sprintf("%s/.gos/data", os.Getenv("HOME"))
- conf.DataDir = config.Env[config.ToString]("GOS_DATA_DIR", conf.DataDir, defaultDataDir)
- conf.ComposeFile = config.Env[config.ToString]("GOS_COMPOSE_FILE", conf.ComposeFile, "compose.txt")
+ conf.DataDir = config.Env[config.Str]("GOS_DATA_DIR", conf.DataDir, defaultDataDir)
+ conf.ComposeFile = config.Env[config.Str]("GOS_COMPOSE_FILE", conf.ComposeFile, "compose.txt")
defaultLogFile := fmt.Sprintf("%s/.gos/gos.log", os.Getenv("HOME"))
- conf.LogFile = config.Env[config.ToString]("GOS_LOG_FILE", conf.LogFile, defaultLogFile)
+ conf.LogFile = config.Env[config.Str]("GOS_LOG_FILE", conf.LogFile, defaultLogFile)
return conf, nil
}
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 1c80fd5..588bd2f 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -14,7 +14,7 @@ func TestEnvToStr(t *testing.T) {
var (
expected = "foobarbaz"
- got = Env[ToString]("GOS_TEST_FROM_ENV")
+ got = Env[Str]("GOS_TEST_FROM_ENV")
)
if got != expected {
@@ -22,18 +22,18 @@ func TestEnvToStr(t *testing.T) {
}
expected = "default value"
- got = Env[ToString]("NON_EXISTENT_ENV", expected)
+ got = Env[Str]("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
}
- if got = Env[ToString]("NON_EXISTENT_ENV"); got != "" {
+ if got = Env[Str]("NON_EXISTENT_ENV"); got != "" {
t.Errorf("got '%s' but expected empty string", got)
}
expected = "casio g-shock"
os.Setenv("GOS_WATCH", expected)
- got = Env[ToString]("GOS_WATCH", "", "", "", expected, "")
+ got = Env[Str]("GOS_WATCH", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
}
@@ -46,25 +46,25 @@ func TestEnvToStrSlice(t *testing.T) {
var (
expected = []string{"foo", "bar", "baz"}
- got = Env[ToStringSlice]("GOS_TEST_SLICE_FROM_ENV")
+ got = Env[StrSlice]("GOS_TEST_SLICE_FROM_ENV")
)
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
expected = []string{"default value"}
- got = Env[ToStringSlice]("NON_EXISTENT_ENV_SLICE", "default value")
+ got = Env[StrSlice]("NON_EXISTENT_ENV_SLICE", "default value")
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
os.Unsetenv("NON_EXISTENT_ENV")
- if got = Env[ToStringSlice]("NON_EXISTENT_ENV"); len(got) > 0 {
+ if got = Env[StrSlice]("NON_EXISTENT_ENV"); len(got) > 0 {
t.Errorf("got '%s' of len '%d' but expected empty slice", got, len(got))
}
expected = []string{"casio", "g-shock"}
- got = Env[ToStringSlice]("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "")
+ got = Env[StrSlice]("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "")
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
@@ -78,7 +78,7 @@ func TestEnvToInt(t *testing.T) {
var (
expected = 1
- got = Env[ToInteger](t, "GOS_TEST_INT_FROM_ENV")
+ got = Env[Int](t, "GOS_TEST_INT_FROM_ENV")
)
if got != expected {
@@ -86,17 +86,17 @@ func TestEnvToInt(t *testing.T) {
}
expected = 999
- got = Env[ToInteger]("NON_EXISTENT_ENV", expected)
+ got = Env[Int]("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%d' but expected '%d'", got, expected)
}
- if got = Env[ToInteger]("NON_EXISTENT_ENV"); got != 0 {
+ if got = Env[Int]("NON_EXISTENT_ENV"); got != 0 {
t.Errorf("got '%d' but expected zero", got)
}
expected = 1234
- got = Env[ToInteger]("GOS_WATCH", "", "", "", expected, "")
+ got = Env[Int]("GOS_WATCH", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%d' but expected '%d'", got, expected)
}
@@ -110,7 +110,7 @@ func TestEnvToBool(t *testing.T) {
var (
expected = true
- got = Env[ToBool]("GOS_TEST_BOOL_FROM_ENV")
+ got = Env[Bool]("GOS_TEST_BOOL_FROM_ENV")
)
if got != expected {
@@ -118,17 +118,17 @@ func TestEnvToBool(t *testing.T) {
}
expected = false
- got = Env[ToBool]("NON_EXISTENT_ENV", expected)
+ got = Env[Bool]("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
- if got = Env[ToBool]("NON_EXISTENT_ENV"); got {
+ if got = Env[Bool]("NON_EXISTENT_ENV"); got {
t.Errorf("got '%t' but expected false", got)
}
expected = true
- got = Env[ToBool]("NON_EXISTENT_ENV", "", "", "", expected, "")
+ got = Env[Bool]("NON_EXISTENT_ENV", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
@@ -142,7 +142,7 @@ func TestSecondENV(t *testing.T) {
var (
expected = "hx"
- got = Env[ToString]("GOS_NONEXISTANT", "EDITOR", "notepad.exe")
+ got = Env[Str]("GOS_NONEXISTANT", "EDITOR", "notepad.exe")
)
if expected != got {
@@ -168,7 +168,7 @@ func TestDefaultStrCB(t *testing.T) {
var (
expected = "hello"
- got = Env[ToString]("GOS_NONEXISTANT", func() string {
+ got = Env[Str]("GOS_NONEXISTANT", func() string {
return "hello"
})
)
@@ -184,7 +184,7 @@ func TestDefaultIntCB(t *testing.T) {
var (
expected = 666
- got = Env[ToInteger]("GOS_NONEXISTANT", func() int {
+ got = Env[Int]("GOS_NONEXISTANT", func() int {
return 666
})
)
diff --git a/internal/config/enver.go b/internal/config/enver.go
index 0cde181..308398f 100644
--- a/internal/config/enver.go
+++ b/internal/config/enver.go
@@ -45,19 +45,19 @@ func Env[U enver[T], T enverConstraint](keys ...any) T {
return enver.zero()
}
-type ToString struct{}
+type Str struct{}
-func (ToString) fromStr(str string) (string, error) {
+func (Str) fromStr(str string) (string, error) {
return str, nil
}
-func (ToString) zero() string {
+func (Str) zero() string {
return ""
}
-type ToStringSlice struct{}
+type StrSlice struct{}
-func (s ToStringSlice) fromStr(str string) ([]string, error) {
+func (s StrSlice) fromStr(str string) ([]string, error) {
result := strings.Split(str, ",")
if len(result) == 1 && result[0] == "" {
return s.zero(), nil
@@ -65,26 +65,26 @@ func (s ToStringSlice) fromStr(str string) ([]string, error) {
return result, nil
}
-func (ToStringSlice) zero() []string {
+func (StrSlice) zero() []string {
return []string{}
}
-type ToInteger struct{}
+type Int struct{}
-func (ToInteger) fromStr(str string) (int, error) {
+func (Int) fromStr(str string) (int, error) {
return strconv.Atoi(str)
}
-func (ToInteger) zero() int {
+func (Int) zero() int {
return 0
}
-type ToBool struct{}
+type Bool struct{}
-func (ToBool) fromStr(str string) (bool, error) {
+func (Bool) fromStr(str string) (bool, error) {
return strconv.ParseBool(str)
}
-func (ToBool) zero() bool {
+func (Bool) zero() bool {
return false
}
diff --git a/internal/config/server/server.go b/internal/config/server/server.go
index 48dd632..1a5d40f 100644
--- a/internal/config/server/server.go
+++ b/internal/config/server/server.go
@@ -35,14 +35,14 @@ func New(configFile, secretsFile string) (ServerConfig, error) {
return conf, err
}
- conf.ListenAddr = config.Env[config.ToString]("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
- conf.Partners = config.Env[config.ToStringSlice]("GOS_PARTNERS", conf.Partners)
- conf.APIKey = config.Env[config.ToString]("GOS_API_KEY", conf.APIKey)
- conf.DataDir = config.Env[config.ToString]("GOS_DATA_DIR", conf.DataDir, "data")
- conf.EmailTo = config.Env[config.ToString]("GOS_EMAIL_TO", conf.EmailTo)
- conf.EmailFrom = config.Env[config.ToString]("GOS_EMAIL_FROM", conf.EmailFrom)
-
- conf.SMTPServer = config.Env[config.ToString]("GOS_SMTP_SERVER", conf.SMTPServer, func() string {
+ conf.ListenAddr = config.Env[config.Str]("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
+ conf.Partners = config.Env[config.StrSlice]("GOS_PARTNERS", conf.Partners)
+ conf.APIKey = config.Env[config.Str]("GOS_API_KEY", conf.APIKey)
+ conf.DataDir = config.Env[config.Str]("GOS_DATA_DIR", conf.DataDir, "data")
+ conf.EmailTo = config.Env[config.Str]("GOS_EMAIL_TO", conf.EmailTo)
+ conf.EmailFrom = config.Env[config.Str]("GOS_EMAIL_FROM", conf.EmailFrom)
+
+ conf.SMTPServer = config.Env[config.Str]("GOS_SMTP_SERVER", conf.SMTPServer, func() string {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
@@ -51,8 +51,8 @@ func New(configFile, secretsFile string) (ServerConfig, error) {
})
const oneHour = 3600
- conf.MergeIntervalS = config.Env[config.ToInteger]("GOS_MERGE_INTERVAL", oneHour)
- conf.ScheduleIntervalS = config.Env[config.ToInteger]("GOS_SCHEDULER_INTERVAL", oneHour*6)
+ conf.MergeIntervalS = config.Env[config.Int]("GOS_MERGE_INTERVAL", oneHour)
+ conf.ScheduleIntervalS = config.Env[config.Int]("GOS_SCHEDULER_INTERVAL", oneHour*6)
return conf, nil
}