-
Notifications
You must be signed in to change notification settings - Fork 10
/
list.go
100 lines (82 loc) · 1.84 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
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
96
97
98
99
100
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func cmdList(cmd *cobra.Command, args []string) error {
if flagAll {
return listAll()
}
return listDir(args)
}
func listAll() error {
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
svc := s3.New(sess)
// list all items in bucket
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(conf.AWS.Bucket)})
if err != nil {
return err
}
files := []string{}
for _, item := range resp.Contents {
key := *item.Key
files = append(files, key)
}
// Check if there is anything in the bucket, if not then return
if len(files) == 0 {
fmt.Printf("no files in %s bucket, nothing to list\n", conf.AWS.Bucket)
return nil
}
last := ""
sep := "----------------------------------------"
fmt.Println(sep)
for _, f := range files {
fDir := filepath.Dir(f)
if fDir != filepath.Dir(last) && !strings.HasPrefix(fDir, last) {
fmt.Println(sep)
}
fmt.Println(f)
last = f
}
fmt.Println(sep)
return nil
}
func listDir(args []string) error {
if len(args) != 2 {
fmt.Println("must specify args: <chain name> <key name>")
return nil
}
chainName := args[0]
keyName := args[1]
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
svc := s3.New(sess)
filePath := filepath.Join(chainName, keyName)
// list all items in bucket
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(conf.AWS.Bucket)})
if err != nil {
return err
}
files := []string{}
for _, item := range resp.Contents {
key := *item.Key
if strings.HasPrefix(key, filePath) {
files = append(files, key)
}
}
for _, f := range files {
fmt.Println(f)
}
return nil
}