-
Notifications
You must be signed in to change notification settings - Fork 2
/
persistent.go
67 lines (57 loc) · 1.37 KB
/
persistent.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
62
63
64
65
66
67
package main
import (
"encoding/json"
"os"
)
type PersistentData struct {
// Map of MACs to names
Clients map[string]PersistentClient `json:"clients"`
LogLocations []string `json:"logLocations"`
}
type PersistentClient struct {
Name string `json:"name"`
}
var persistent PersistentData
const PERSISTENT_LOCATION = "slimytm_persistent.json"
func LoadPersistent() {
f, err := os.Open(PERSISTENT_LOCATION)
if os.IsNotExist(err) {
f, err = os.Create(PERSISTENT_LOCATION)
if err != nil {
logger.Panicw("unable to create persistent data file",
"location", PERSISTENT_LOCATION,
"err", err)
}
f.Write([]byte("{}"))
f.Seek(0, 0)
} else if err != nil {
logger.Panicw("unable to open persistent data file",
"location", PERSISTENT_LOCATION,
"err", err)
}
defer f.Close()
d := json.NewDecoder(f)
err = d.Decode(&persistent)
if err != nil {
logger.Panicw("unable to parse persistent data",
"location", PERSISTENT_LOCATION,
"err", err)
}
}
func SavePersistent() {
f, err := os.Create(PERSISTENT_LOCATION)
if err != nil {
logger.DPanicw("unable to save persistent data",
"location", PERSISTENT_LOCATION,
"err", err)
return
}
defer f.Close()
e := json.NewEncoder(f)
err = e.Encode(&persistent)
if err != nil {
logger.DPanicw("unable to encode persistent data",
"location", PERSISTENT_LOCATION,
"err", err)
}
}