-
Notifications
You must be signed in to change notification settings - Fork 1
/
combined.go
77 lines (69 loc) · 1.57 KB
/
combined.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 goxr
import (
"bytes"
"errors"
"github.com/echocat/goxr/common"
"os"
)
type CombinedBox []Box
func (instance CombinedBox) Open(name string) (common.File, error) {
for _, box := range instance {
if f, err := box.Open(name); os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
} else {
return f, nil
}
}
return nil, common.NewPathError("open", name, os.ErrNotExist)
}
func (instance CombinedBox) Info(name string) (common.FileInfo, error) {
for _, box := range instance {
if fi, err := box.Info(name); os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
} else {
return fi, nil
}
}
return nil, common.NewPathError("info", name, os.ErrNotExist)
}
func (instance CombinedBox) Close() error {
var errs []error
for _, box := range instance {
if err := box.Close(); err != nil {
errs = append(errs, err)
}
}
if len(errs) <= 0 {
return nil
} else if len(errs) == 1 {
return errs[0]
} else {
buf := new(bytes.Buffer)
for i, err := range errs {
if i > 0 {
common.MustWritef(buf, "\nAND ")
}
common.MustWritef(buf, "%v", err)
}
return errors.New(buf.String())
}
}
func (instance CombinedBox) ForEach(predicate common.FilePredicate, callback func(common.FileInfo) error) error {
for _, box := range instance {
if ib, ok := box.(Iterable); ok {
if err := ib.ForEach(predicate, callback); err != nil {
return err
}
} else {
return ErrBoxIterationNotSupported
}
}
return nil
}
func (instance CombinedBox) With(box Box) CombinedBox {
return append(instance, box)
}