From f0c0d0ba94c923ca06b49211c70db47616e5a8e1 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 19 Nov 2019 22:25:14 +0200 Subject: Save sessions on exit --- persistence/sessions.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'persistence') diff --git a/persistence/sessions.go b/persistence/sessions.go index 36fac52..ab46823 100644 --- a/persistence/sessions.go +++ b/persistence/sessions.go @@ -6,6 +6,7 @@ import ( "dev.narayana.im/narayana/telegabber/yamldb" + log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) @@ -44,6 +45,10 @@ func LoadSessions(path string) (SessionsYamlDB, error) { return sessionDB, nil } +func emptySessionsMap(dataPtr *SessionsMap) { + dataPtr.Sessions = make(map[string]Session) +} + func initYamlDB(path string, dataPtr *SessionsMap) (SessionsYamlDB, error) { file, err := ioutil.ReadFile(path) if err == nil { @@ -51,9 +56,14 @@ func initYamlDB(path string, dataPtr *SessionsMap) (SessionsYamlDB, error) { if err != nil { return SessionsYamlDB{}, errors.Wrap(err, "YamlDB is corrupted") } + + if dataPtr.Sessions == nil { + emptySessionsMap(dataPtr) + } + log.Debugf("Unmarshalled YAML: %#v", *dataPtr) } else { // DB file does not exist, create an empty DB - dataPtr.Sessions = make(map[string]Session) + emptySessionsMap(dataPtr) } return SessionsYamlDB{ -- cgit v1.2.3