forked from WasabiAiR/stow
-
Notifications
You must be signed in to change notification settings - Fork 10
/
walk.go
81 lines (76 loc) · 2.02 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
74
75
76
77
78
79
80
81
package stow
// DEV NOTE: tests for this are in test/test.go
// WalkFunc is a function called for each Item visited
// by Walk.
// If there was a problem, the incoming error will describe
// the problem and the function can decide how to handle
// that error.
// If an error is returned, processing stops.
type WalkFunc func(item Item, err error) error
// Walk walks all Items in the Container.
// Returns the first error returned by the WalkFunc or
// nil if no errors were returned.
// The pageSize is the number of Items to get per request.
func Walk(container Container, prefix string, pageSize int, fn WalkFunc) error {
var (
err error
items []Item
cursor = CursorStart
)
for {
items, cursor, err = container.Items(prefix, cursor, pageSize)
if err != nil {
err = fn(nil, err)
if err != nil {
return err
}
}
for _, item := range items {
err = fn(item, nil)
if err != nil {
return err
}
}
if IsCursorEnd(cursor) {
break
}
}
return nil
}
// WalkContainersFunc is a function called for each Container visited
// by WalkContainers.
// If there was a problem, the incoming error will describe
// the problem and the function can decide how to handle
// that error.
// If an error is returned, processing stops.
type WalkContainersFunc func(container Container, err error) error
// WalkContainers walks all Containers in the Location.
// Returns the first error returned by the WalkContainersFunc or
// nil if no errors were returned.
// The pageSize is the number of Containers to get per request.
func WalkContainers(location Location, prefix string, pageSize int, fn WalkContainersFunc) error {
var (
err error
containers []Container
cursor = CursorStart
)
for {
containers, cursor, err = location.Containers(prefix, cursor, pageSize)
if err != nil {
err = fn(nil, err)
if err != nil {
return err
}
}
for _, container := range containers {
err = fn(container, nil)
if err != nil {
return err
}
}
if IsCursorEnd(cursor) {
break
}
}
return nil
}