diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-19 23:25:14 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-19 23:25:14 +0300 |
commit | f0c0d0ba94c923ca06b49211c70db47616e5a8e1 (patch) | |
tree | 23c097b6cb8249d4f978f7b22bd1b9877b0a5bba /persistence | |
parent | a09817976ee5cdb7d1bca25c80cdef3d1b7867f7 (diff) |
Save sessions on exit
Diffstat (limited to 'persistence')
-rw-r--r-- | persistence/sessions.go | 12 |
1 files changed, 11 insertions, 1 deletions
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{ |