-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
62 lines (57 loc) · 1.62 KB
/
list.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
package afs
import (
"context"
"github.com/viant/afs/file"
"github.com/viant/afs/matcher"
"github.com/viant/afs/option"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
)
func (s *service) List(ctx context.Context, URL string, options ...storage.Option) ([]storage.Object, error) {
URL = url.Normalize(URL, file.Scheme)
recursive := &option.Recursive{}
options, _ = option.Assign(options, &recursive)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return nil, err
}
var result = make([]storage.Object, 0)
return result, list(ctx, manager, URL, recursive.Flag, options, &result)
}
func list(ctx context.Context, lister storage.Lister, URL string, recursive bool, options []storage.Option, result *[]storage.Object) error {
objects, err := lister.List(ctx, URL, options...)
if err != nil {
return err
}
dirs := make([]storage.Object, 0)
for i, object := range objects {
if object.IsDir() && recursive {
if !url.Equals(URL, object.URL()) {
dirs = append(dirs, objects[i])
}
continue
}
*result = append(*result, objects[i])
}
if recursive {
var matchOpt option.Match
var matcherOpt option.Matcher
if _, has := option.Assign(options, &matcherOpt, &matchOpt); has {
dirMatcher := &matcher.Basic{Directory: &recursive}
dirs, err = lister.List(ctx, URL, dirMatcher.Match)
if err != nil {
return err
}
}
for i := 0; i < len(dirs); i++ {
if i == 0 && url.Equals(URL, dirs[i].URL()) {
continue
}
*result = append(*result, dirs[i])
if err = list(ctx, lister, dirs[i].URL(), recursive, options, result); err != nil {
return err
}
}
}
return nil
}