-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskfile.go
67 lines (55 loc) · 1.26 KB
/
taskfile.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 (
"io"
"os"
pomoprotos "github.com/scjudd/pomodoro/protos"
"google.golang.org/protobuf/proto"
)
func save(filename string, tasklist []*task) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
protolist := &pomoprotos.TaskList{
Items: make([]*pomoprotos.Task, 0, len(tasklist)),
}
for _, task := range tasklist {
protolist.Items = append(protolist.Items, &pomoprotos.Task{
Pomodoros: int32(task.pomodoros),
Completed: int32(task.completed),
Description: task.description,
})
}
bytes, err := proto.Marshal(protolist)
if err != nil {
return err
}
_, err = f.Write(bytes)
return err
}
func load(filename string) ([]*task, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
return nil, err
}
protolist := &pomoprotos.TaskList{}
err = proto.Unmarshal(bytes, protolist)
if err != nil {
return nil, err
}
tasklist := make([]*task, 0, len(protolist.Items))
for _, prototask := range protolist.Items {
tasklist = append(tasklist, &task{
pomodoros: int(prototask.Pomodoros),
completed: int(prototask.Completed),
description: prototask.Description,
})
}
return tasklist, nil
}