-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary_cache.go
88 lines (73 loc) · 1.85 KB
/
summary_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package exoskeleton
import (
"encoding/json"
"os"
)
type summaryCache struct {
Path string
data *cache
onError func(error)
}
type cache struct {
Summary map[string]cachedValue `json:"summary"`
}
type cachedValue struct {
ModTime int64 `json:"modTime"`
Value string `json:"value"`
}
func (c *summaryCache) load() {
c.data = &cache{}
// cache is not configured
if c.Path == "" {
return
}
if b, err := os.ReadFile(c.Path); os.IsNotExist(err) {
// cache file just does not exist yet
return
} else if err != nil {
c.onError(CacheError{Cause: err, Message: "could not load cache"})
} else if err := json.Unmarshal(b, &c.data); err != nil {
c.onError(CacheError{Cause: err, Message: "could not load cache"})
}
}
func (c *summaryCache) dump() {
if c.Path == "" {
return
}
if b, err := json.Marshal(c.data); err != nil {
c.onError(CacheError{Cause: err, Message: "could not write cache"})
} else if err = os.WriteFile(c.Path, b, 0644); err != nil {
c.onError(CacheError{Cause: err, Message: "could not write cache"})
}
}
func (c *summaryCache) Read(cmd Command) (string, error) {
if _, ok := cmd.(*builtinCommand); ok {
return cmd.Summary()
}
if c.data == nil {
c.load()
}
cacheKey := Usage(cmd)
modTime, err := modTime(cmd)
if err != nil {
c.onError(CacheError{Cause: err, Message: "skipping cache for " + cmd.Path()})
} else if item, ok := c.data.Summary[cacheKey]; ok && item.ModTime == modTime {
return item.Value, nil
}
summary, err := cmd.Summary()
if err == nil {
if c.data.Summary == nil {
c.data.Summary = make(map[string]cachedValue)
}
c.data.Summary[cacheKey] = cachedValue{ModTime: modTime, Value: summary}
c.dump()
}
return summary, err
}
func modTime(cmd Command) (int64, error) {
if info, err := os.Stat(cmd.Path()); err != nil {
return -1, err
} else {
return info.ModTime().Unix(), nil
}
}