-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (74 loc) · 2.04 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"github.com/aws/aws-sdk-go/aws/session"
"fmt"
"github.com/tehmoon/errors"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
const (
PREFIXES = "/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!-_.*'()"
OUTPUT_LINE Output = iota
OUTPUT_CSV
OUTPUT_JSON_LINE
)
type Output uint8
func main() {
options, err := ParseFlags()
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrap(err, "Error parsing flags").Error())
os.Exit(2)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := s3.New(sess)
sync := make(chan *Directory)
for _, prefix := range PREFIXES {
p := fmt.Sprintf("%s%s", options.Prefix, string(prefix))
go fetchPrefix(svc, options.Bucket, p, sync)
}
root := NewRootDirectory()
for range PREFIXES {
MergeDirectories(root, <- sync)
}
err = OutputTree(root, options.Depth, options.Template)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}
func fetchPrefix(svc *s3.S3, bucket, prefix string, syncMaster chan *Directory) {
var (
pages uint64 = 0
sync = make(chan *Directory)
objectVersionsInput = &s3.ListObjectVersionsInput{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
}
)
err := svc.ListObjectVersionsPages(objectVersionsInput, func (page *s3.ListObjectVersionsOutput, lastPage bool) (bool) {
pages++
go buildDirectory(page.Versions, sync)
return ! lastPage
})
if err != nil {
fmt.Printf("FATAL Error: %s\n", err.Error())
os.Exit(1)
}
root := NewRootDirectory()
for i := uint64(0); i < pages; i++ {
MergeDirectories(root, <- sync)
}
syncMaster <- root
}
func buildDirectory(versions []*s3.ObjectVersion, sync chan *Directory) {
root := NewRootDirectory()
for _, version := range versions {
key := *version.Key
if *version.IsLatest && key[len(key) - 1] != '/' {
root.CreateFullPathFile(*version.Key, *version.Size)
}
}
sync <- root
}