-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
77 lines (63 loc) · 1.42 KB
/
utils.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
package dash
import (
"io"
"path/filepath"
"strings"
)
func spellHas(spell []string, token string) bool {
for _, tok := range spell {
if tok == token {
return true
}
}
return false
}
func pathDepth(path string) int {
return len(strings.Split(path, "/"))
}
func hasExt(path string, ext string) bool {
return strings.HasSuffix(strings.ToLower(path), ext)
}
func getExt(path string) string {
return strings.ToLower(filepath.Ext(path))
}
// Adapt an io.ReadSeeker into an io.ReaderAt in the dumbest possible fashion
type readerAtFromSeeker struct {
rs io.ReadSeeker
}
var _ io.ReaderAt = (*readerAtFromSeeker)(nil)
func (r *readerAtFromSeeker) ReadAt(b []byte, off int64) (int, error) {
_, err := r.rs.Seek(off, io.SeekStart)
if err != nil {
return 0, err
}
return r.rs.Read(b)
}
func selectByFlavor(candidates []*Candidate, f Flavor) []*Candidate {
res := make([]*Candidate, 0)
for _, c := range candidates {
if c.Flavor == f {
res = append(res, c)
}
}
return res
}
func selectByArch(candidates []*Candidate, a Arch) []*Candidate {
res := make([]*Candidate, 0)
for _, c := range candidates {
if c.Arch == a {
res = append(res, c)
}
}
return res
}
type candidateFilter func(candidate *Candidate) bool
func selectByFunc(candidates []*Candidate, f candidateFilter) []*Candidate {
res := make([]*Candidate, 0)
for _, c := range candidates {
if f(c) {
res = append(res, c)
}
}
return res
}