-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
46 lines (38 loc) · 1.06 KB
/
file.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
package main
import (
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func (bot *EventBot) saveEvent() {
eventData, err := yaml.Marshal(bot.currentEvent)
if err != nil {
logrus.Errorf("Could not marshal current event: %s", err)
}
err = ioutil.WriteFile(bot.EventFilePath, eventData, 0644)
if err != nil {
logrus.Errorf("Could not save event file %s: %s", bot.EventFilePath, err)
}
logrus.Infof("Saved event file %s", bot.EventFilePath)
}
func (bot *EventBot) loadEventFromFile() {
eventData, err := ioutil.ReadFile(bot.EventFilePath)
if err != nil {
logrus.Warningf("Could not read event file %s: %s", bot.EventFilePath, err)
return
}
bot.currentEvent = &Event{}
err = yaml.Unmarshal(eventData, bot.currentEvent)
if err != nil {
logrus.Errorf("Could not unmarshal saved event: %s", err)
return
}
logrus.Infof("Read event file %s", bot.EventFilePath)
}
func (bot *EventBot) removeEventFile() {
err := os.Remove(bot.EventFilePath)
if err != nil {
logrus.Errorf("Could not remove event file %s: %s", bot.EventFilePath, err)
}
}