-
Notifications
You must be signed in to change notification settings - Fork 46
/
cmd_ls.go
68 lines (55 loc) · 1.48 KB
/
cmd_ls.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/urfave/cli/v2"
)
func ListBucket(config *Config, c *cli.Context) error {
args := c.Args().Slice()
svc := SessionNew(config)
if len(args) == 0 {
var params *s3.ListBucketsInput
resp, err := svc.ListBuckets(params)
if err != nil {
return err
}
for _, bucket := range resp.Buckets {
fmt.Printf("%s s3://%s\n", bucket.CreationDate.Format(DATE_FMT), *bucket.Name)
}
return nil
}
return listBucket(config, svc, args)
}
func listBucket(config *Config, svc *s3.S3, args []string) error {
for _, arg := range args {
u, err := FileURINew(arg)
if err != nil || u.Scheme != "s3" {
return fmt.Errorf("ls requires buckets to be prefixed with s3://")
}
_, err = SessionForBucket(config, u.Bucket)
if err != nil {
return err
}
todo := []string{arg}
for len(todo) != 0 {
var item string
item, todo = todo[0], todo[1:]
remotePager(config, svc, item, !config.Recursive, func(page *s3.ListObjectsV2Output) {
for _, item := range page.CommonPrefixes {
uri := fmt.Sprintf("s3://%s/%s", u.Bucket, *item.Prefix)
if config.Recursive {
todo = append(todo, uri)
} else {
fmt.Printf("%16s %9s %s\n", "", "DIR", uri)
}
}
if page.Contents != nil {
for _, item := range page.Contents {
fmt.Printf("%16s %9d s3://%s/%s\n", item.LastModified.Format(DATE_FMT), *item.Size, u.Bucket, *item.Key)
}
}
})
}
}
return nil
}