-
Notifications
You must be signed in to change notification settings - Fork 1
/
appendlog.go
60 lines (48 loc) · 933 Bytes
/
appendlog.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
package main
import (
"encoding/json"
"os"
"time"
)
type Log map[string]time.Time
type AppendLog struct {
log Log
filename string
}
func loadAppendLog(filename string) (*AppendLog, error) {
appendLog := &AppendLog{
log: Log{},
filename: filename,
}
file, err := os.Open(filename)
if err != nil {
// file doesnt exist FIXME
return appendLog, nil
}
defer file.Close()
decoder := json.NewDecoder(file)
if err = decoder.Decode(&appendLog.log); err != nil {
return nil, err
} else {
return appendLog, nil
}
}
func (l *AppendLog) Add(key string) {
l.log[key] = time.Now()
}
func (l *AppendLog) Exists(key string) bool {
_, ok := l.log[key]
return ok
}
func (l *AppendLog) Save() error {
file, err := os.Create(l.filename)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
if err = encoder.Encode(&l.log); err != nil {
return err
}
return nil
}