1
0
Fork 0

Get persistence file path from $STATE_DIRECTORY

This commit is contained in:
Michał Rudowicz 2024-03-03 13:54:42 +01:00
parent 765a41f8a1
commit b19ae59a9c
2 changed files with 28 additions and 1 deletions

11
main.go
View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -114,6 +115,14 @@ func makeSatel(satelAddr string) *satel.Satel {
return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10}) 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() { func main() {
var ( var (
wg sync.WaitGroup wg sync.WaitGroup
@ -137,7 +146,7 @@ func main() {
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) 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( Consume(
SendToTg( SendToTg(

18
main_test.go Normal file
View File

@ -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)
}