summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-03 10:04:04 +0300
committerPaul Buetow <paul@buetow.org>2024-09-03 10:04:04 +0300
commitc907dd82c9ba2e7b1924b9f4febba68f9b04a38d (patch)
treee5bcf380afdd757d43fbefa56d610b804667d9e9
parentc1432a3f7fb1d454fbcfa047b0c37a95a16c9e33 (diff)
add EnvToStrSlice
-rw-r--r--internal/config/config.go10
-rw-r--r--internal/config/config_test.go56
-rw-r--r--internal/config/server/server.go22
-rw-r--r--internal/server/repository/repository.go1
-rw-r--r--internal/server/scheduler/scheduler.go2
5 files changed, 70 insertions, 21 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index aa08e6e..f71911b 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -5,6 +5,7 @@ import (
"io"
"os"
"strconv"
+ "strings"
"unicode"
)
@@ -48,6 +49,14 @@ func EnvToStr(keys ...any) string {
return ""
}
+func EnvToStrSlice(keys ...any) []string {
+ result := strings.Split(EnvToStr(keys...), ",")
+ if len(result) == 1 && result[0] == "" {
+ return []string{}
+ }
+ return result
+}
+
func EnvToInt(keys ...any) int {
for _, key := range keys {
switch key := key.(type) {
@@ -72,7 +81,6 @@ func EnvToInt(keys ...any) int {
return 0
}
-// TODO: Maybe the EnvTo... can be programmed more generic...?
func EnvToBool(keys ...any) bool {
for _, key := range keys {
switch key := key.(type) {
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 41e9851..f56d44f 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -2,12 +2,14 @@ package config
import (
"os"
+ "slices"
"testing"
)
func TestEnvToStr(t *testing.T) {
t.Parallel()
+ os.Unsetenv("NON_EXISTENT_ENV")
os.Setenv("GOS_TEST_FROM_ENV", "foobarbaz")
var (
@@ -21,14 +23,13 @@ func TestEnvToStr(t *testing.T) {
t.Logf("got '%s' as expected", expected)
expected = "default value"
- got = EnvToStr("GOS_JAJAJA", expected)
+ got = EnvToStr("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
}
t.Logf("got '%s' as expected", expected)
- os.Unsetenv("JUJUJU_NOT_EXISTANT_ENV")
- if got = EnvToStr("JUJUJU_NOT_EXISTANT_ENV"); got != "" {
+ if got = EnvToStr("NON_EXISTENT_ENV"); got != "" {
t.Errorf("got '%s' but expected empty string", got)
}
t.Logf("got empty string as expected")
@@ -41,9 +42,45 @@ func TestEnvToStr(t *testing.T) {
t.Logf("got '%s' as expected", expected)
}
+func TestEnvToStrSlice(t *testing.T) {
+ t.Parallel()
+
+ os.Setenv("GOS_TEST_SLICE_FROM_ENV", "foo,bar,baz")
+
+ var (
+ expected = []string{"foo", "bar", "baz"}
+ got = EnvToStrSlice("GOS_TEST_SLICE_FROM_ENV")
+ )
+ if !slices.Equal(got, expected) {
+ t.Errorf("got '%v' but expected '%v'", got, expected)
+ }
+ t.Logf("got '%v' as expected", expected)
+
+ expected = []string{"default value"}
+ got = EnvToStrSlice("NON_EXISTENT_ENV_SLICE", "default value")
+ if !slices.Equal(got, expected) {
+ t.Errorf("got '%v' but expected '%v'", got, expected)
+ }
+ t.Logf("got '%v' as expected", expected)
+
+ os.Unsetenv("NON_EXISTENT_ENV")
+ if got = EnvToStrSlice("NON_EXISTENT_ENV"); len(got) > 0 {
+ t.Errorf("got '%s' of len '%d' but expected empty slice", got, len(got))
+ }
+ t.Logf("got empty slice as expected")
+
+ expected = []string{"casio", "g-shock"}
+ got = EnvToStrSlice("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "")
+ if !slices.Equal(got, expected) {
+ t.Errorf("got '%v' but expected '%v'", got, expected)
+ }
+ t.Logf("got '%v' as expected", expected)
+}
+
func TestEnvToInt(t *testing.T) {
t.Parallel()
+ os.Unsetenv("NON_EXISTENT_ENV")
os.Setenv("GOS_TEST_INT_FROM_ENV", "1")
var (
@@ -57,14 +94,13 @@ func TestEnvToInt(t *testing.T) {
t.Logf("got '%d' as expected", expected)
expected = 999
- got = EnvToInt("GOS_JAJAJA", expected)
+ got = EnvToInt("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%d' but expected '%d'", got, expected)
}
t.Logf("got '%d' as expected", expected)
- os.Unsetenv("JUJUJU_NOT_EXISTANT_ENV")
- if got = EnvToInt("JUJUJU_NOT_EXISTANT_ENV"); got != 0 {
+ if got = EnvToInt("NON_EXISTENT_ENV"); got != 0 {
t.Errorf("got '%d' but expected zero", got)
}
t.Logf("got zero as expected")
@@ -80,6 +116,7 @@ func TestEnvToInt(t *testing.T) {
func TestEnvToBool(t *testing.T) {
t.Parallel()
+ os.Unsetenv("NON_EXISTENT_ENV")
os.Setenv("GOS_TEST_BOOL_FROM_ENV", "true")
var (
@@ -93,20 +130,19 @@ func TestEnvToBool(t *testing.T) {
t.Logf("got '%t' as expected", expected)
expected = false
- got = EnvToBool("GOS_JAJAJA", expected)
+ got = EnvToBool("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
t.Logf("got '%t' as expected", expected)
- os.Unsetenv("JUJUJU_NOT_EXISTANT_ENV")
- if got = EnvToBool("JUJUJU_NOT_EXISTANT_ENV"); got {
+ if got = EnvToBool("NON_EXISTENT_ENV"); got {
t.Errorf("got '%t' but expected false", got)
}
t.Logf("got 'false' as expected")
expected = true
- got = EnvToBool("JUJUJU_NOT_EXISTANT_ENV", "", "", "", expected, "")
+ got = EnvToBool("NON_EXISTENT_ENV", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
diff --git a/internal/config/server/server.go b/internal/config/server/server.go
index 1032a9f..1041a81 100644
--- a/internal/config/server/server.go
+++ b/internal/config/server/server.go
@@ -10,16 +10,17 @@ import (
)
type ServerConfig struct {
- ListenAddr string `json:"ListenAddr,omitempty"`
- Partner string `json:"Partner,omitempty"`
- APIKey string `json:"APIKey,omitempty"`
- DataDir string `json:"StateDir,omitempty"`
- EmailTo string `json:"EmailTo,omitempty"`
- EmailFrom string `json:"EmailFrom,omitempty"`
- SMTPServer string `json:"SMTPServer,omitempty"`
- MergeIntervalS int `json:"MergeInterval,omitempty"`
- ScheduleIntervalS int `json:"ScheduleInterval,omitempty"`
- Secrets SecretsConfig `json:"Secrets,omitempty"`
+ ListenAddr string `json:"ListenAddr,omitempty"`
+ Partner string `json:"Partner,omitempty"`
+ APIKey string `json:"APIKey,omitempty"`
+ DataDir string `json:"StateDir,omitempty"`
+ EmailTo string `json:"EmailTo,omitempty"`
+ EmailFrom string `json:"EmailFrom,omitempty"`
+ SMTPServer string `json:"SMTPServer,omitempty"`
+ MergeIntervalS int `json:"MergeInterval,omitempty"`
+ ScheduleIntervalS int `json:"ScheduleInterval,omitempty"`
+ // SocialPlatformsEnable []string `json:"SocialPlatformsEnable,omitempty"`
+ Secrets SecretsConfig `json:"Secrets,omitempty"`
}
func New(configFile, secretsFile string) (ServerConfig, error) {
@@ -36,6 +37,7 @@ func New(configFile, secretsFile string) (ServerConfig, error) {
}
conf.ListenAddr = config.EnvToStr("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
+ // TODO: Return an array or slice
conf.Partner = config.EnvToStr("GOS_PARTNER", "GOS_PARTNERS", conf.Partner)
conf.APIKey = config.EnvToStr("GOS_API_KEY", conf.APIKey)
conf.DataDir = config.EnvToStr("GOS_DATA_DIR", conf.DataDir, "data")
diff --git a/internal/server/repository/repository.go b/internal/server/repository/repository.go
index b99a310..9641ce4 100644
--- a/internal/server/repository/repository.go
+++ b/internal/server/repository/repository.go
@@ -48,6 +48,7 @@ func Instance(conf server.ServerConfig) Repository {
return instance
}
+// Need to register all social platforms for in-memory representation of shared posts and so on.
func newRepository(conf server.ServerConfig, fs fs) Repository {
var loaded bool
return Repository{
diff --git a/internal/server/scheduler/scheduler.go b/internal/server/scheduler/scheduler.go
index 8aae0ed..da66519 100644
--- a/internal/server/scheduler/scheduler.go
+++ b/internal/server/scheduler/scheduler.go
@@ -2,6 +2,8 @@ package scheduler
import "context"
+// TODO: Implement this
func Run(ctx context.Context) error {
+ // Need to figure out which posts to post where.
return nil
}