-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
79 lines (66 loc) · 1.3 KB
/
index.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
package stats
import (
"os"
"sort"
"go.uber.org/multierr"
"github.com/dnovikoff/tenhou/tools/tentool/utils"
)
type FileIndex struct {
utils.JSONGZFile
data map[string]string
}
func NewFileIndex(p string) *FileIndex {
x := &FileIndex{data: map[string]string{}}
x.Path = p
return x
}
func (i *FileIndex) Load() error {
return i.JSONGZFile.Load(&i.data)
}
func (i *FileIndex) Save() (err error) {
return i.JSONGZFile.Save(i.data)
}
func (i *FileIndex) JustAdd(url, path string) {
i.data[url] = path
}
func (i *FileIndex) Add(url, path string) error {
i.JustAdd(url, path)
return i.Save()
}
func (i *FileIndex) Check(url string) bool {
_, found := i.data[url]
return found
}
func (i *FileIndex) Len() int {
return len(i.data)
}
func (i *FileIndex) Files() []string {
out := make([]string, 0, len(i.data))
for _, v := range i.data {
out = append(out, v)
}
sort.Strings(out)
return out
}
func (i *FileIndex) Validate() error {
var total error
checked := make(map[string]bool, len(i.data))
for k, v := range i.data {
found, ok := checked[v]
if !ok {
_, err := os.Stat(v)
if err != nil {
total = multierr.Append(total, err)
checked[v] = false
found = false
} else {
checked[v] = true
found = true
}
}
if !found {
delete(i.data, k)
}
}
return total
}