forked from codahale/sneaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
63 lines (54 loc) · 1.23 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
63
package sneaker
import (
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
// List returns a list of files which match the given pattern, or if the pattern
// is blank, all files.
func (m *Manager) List(pattern string) ([]File, error) {
resp, err := m.Objects.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(m.Bucket),
Prefix: aws.String(m.Prefix),
})
if err != nil {
return nil, err
}
var secrets []File
for _, obj := range resp.Contents {
secrets = append(secrets, File{
Path: (*obj.Key)[len(m.Prefix):len(*obj.Key)],
LastModified: obj.LastModified.In(time.UTC),
Size: int(*obj.Size) - 224, // header + KMS data key
ETag: strings.Replace(*obj.ETag, "\"", "", -1),
})
}
if pattern == "" {
return secrets, nil
}
var matched []File
for _, f := range secrets {
ok, err := match(pattern, f.Path)
if err != nil {
return nil, err
}
if ok {
matched = append(matched, f)
}
}
return matched, nil
}
func match(pattern, name string) (bool, error) {
for _, s := range strings.Split(pattern, ",") {
m, err := path.Match(s, name)
if err != nil {
return false, err
}
if m {
return true, nil
}
}
return false, nil
}