-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatdb.go
61 lines (47 loc) · 1.01 KB
/
chatdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
type ChatDB struct {
Path string
// Map usernames to chat id
Db map[string]int64
}
func InitChatDB(path string) (*ChatDB, error) {
db := make(map[string]int64)
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
defer f.Close()
yamlData, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yamlData, &db)
if err != nil {
return nil, err
}
return &ChatDB{Path: path, Db: db}, nil
}
func (chatdb *ChatDB) UpdateWith(username string, chatId int64) error {
if _, ok := chatdb.Db[username]; !ok {
chatdb.Db[username] = chatId
yamlData, err := yaml.Marshal(chatdb.Db)
if err != nil {
return err
}
err = os.Rename(chatdb.Path, chatdb.Path+".bak")
if err != nil {
log.Printf("Cannot perform a backup of the chatdb before update: %s", err)
}
err = ioutil.WriteFile(chatdb.Path, yamlData, 0600)
if err != nil {
return err
}
}
return nil
}