-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
cache.go
50 lines (45 loc) · 921 Bytes
/
cache.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"github.com/sachaos/todoist/lib"
)
func LoadCache(filename string, s *todoist.Store) error {
err := ReadCache(filename, s)
if err != nil {
err = WriteCache(cachePath, s)
if err != nil {
return err
}
}
return nil
}
func ReadCache(filename string, s *todoist.Store) error {
jsonString, err := ioutil.ReadFile(filename)
if err != nil {
return CommandFailed
}
err = json.Unmarshal(jsonString, &s)
if err != nil {
return CommandFailed
}
s.ConstructItemTree()
return nil
}
func WriteCache(filename string, s *todoist.Store) error {
buf, err := json.MarshalIndent(s, "", " ")
if err != nil {
return CommandFailed
}
err = AssureExists(filename)
if err != nil {
return err
}
err2 := ioutil.WriteFile(filename, buf, os.ModePerm)
if err2 != nil {
return errors.New("Couldn't write to the cache file")
}
return nil
}