-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathwalk.go
73 lines (60 loc) · 1.33 KB
/
walk.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
package bfs
import (
"os"
"github.com/itchio/wharf/tlc"
)
func Walk(path string) (*tlc.Container, error) {
return tlc.WalkDir(path, &tlc.WalkOpts{Filter: DotItchFilter()})
}
func DotItchFilter() tlc.FilterFunc {
return func(fi os.FileInfo) bool {
// skip directories named ".itch". in WalkDir, this
// will also skip all its children
if fi.IsDir() && fi.Name() == ".itch" {
return false
}
// walk everything else
return true
}
}
// ContainerPaths returns a list of all paths in a
// container, for all files and symlinks. Folders
// are excluded
func ContainerPaths(container *tlc.Container) []string {
res := []string{}
for _, f := range container.Files {
res = append(res, f.Path)
}
for _, s := range container.Symlinks {
res = append(res, s.Path)
}
return res
}
// Return elements in b that aren't in a
func Difference(a []string, b []string) []string {
// struct{} = 0-sized type, we're using it to
// use `map` as a set.
aMap := make(map[string]struct{})
for _, el := range a {
aMap[el] = struct{}{}
}
var res = []string{}
for _, el := range b {
if _, ok := aMap[el]; !ok {
res = append(res, el)
}
}
return res
}
func SliceToLength(a []string, length int) []string {
if a == nil {
return a
}
return a[:min(length, len(a))]
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}