Get persistence file path from $STATE_DIRECTORY

command_handler
Michał Rudowicz 2024-03-03 13:54:42 +01:00
rodzic 765a41f8a1
commit b19ae59a9c
2 zmienionych plików z 28 dodań i 1 usunięć

11
main.go
Wyświetl plik

@ -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(

18
main_test.go 100644
Wyświetl plik

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