From b19ae59a9c3b9b3dc68066f60cfd3a1a4593af23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 3 Mar 2024 13:54:42 +0100 Subject: [PATCH] Get persistence file path from $STATE_DIRECTORY --- main.go | 11 ++++++++++- main_test.go | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/main.go b/main.go index 0fd1757..421e586 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net" "os" + "path/filepath" "strconv" "strings" "sync" @@ -114,6 +115,14 @@ func makeSatel(satelAddr string) *satel.Satel { return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10}) } +func getPersistenceFilePath() string { + var stateDir = os.Getenv("STATE_DIRECTORY") + if len(stateDir) != 0 { + return filepath.Join(stateDir, PersistenceFilename) + } + return PersistenceFilename +} + func main() { var ( wg sync.WaitGroup @@ -137,7 +146,7 @@ func main() { tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) - dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), PersistenceFilename) + dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) Consume( SendToTg( diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..8545b6f --- /dev/null +++ b/main_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetPersistenceFilename(t *testing.T) { + var oldStateDir = os.Getenv("STATE_DIRECTORY") + os.Setenv("STATE_DIRECTORY", "test_dir") + assert.Equal(t, filepath.Join("test_dir", PersistenceFilename), getPersistenceFilePath()) + os.Setenv("STATE_DIRECTORY", "") + assert.Equal(t, PersistenceFilename, getPersistenceFilePath()) + os.Setenv("STATE_DIRECTORY", oldStateDir) +}