-
Notifications
You must be signed in to change notification settings - Fork 18
/
sos_list.go
159 lines (124 loc) · 3.55 KB
/
sos_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
const (
printDate = "2006-01-02 15:04:05 MST"
)
var sosListCmd = &cobra.Command{
Use: "list [BUCKET/PATH]",
Short: "List buckets and files",
Long: `This command lists all your buckets or all the files stored in the specified bucket.
Note: the buckets size reported is computed daily, it may not be the actual size at the time of listing.`,
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
isRecursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
isShort, err := cmd.Flags().GetBool("short")
if err != nil {
return err
}
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
if len(args) == 0 {
return displayBuckets(sosClient, isRecursive, isShort)
}
return displayBucket(sosClient, filepath.ToSlash(args[0]), isRecursive, isShort)
},
}
func displayBuckets(sosClient *sosClient, isRecursive, isShort bool) error {
resp, err := cs.RequestWithContext(gContext, egoscale.ListBucketsUsage{})
if err != nil {
return err
}
buckets := resp.(*egoscale.ListBucketsUsageResponse)
table := tabwriter.NewWriter(os.Stdout, 10, 0, 1, ' ', tabwriter.TabIndent)
for _, b := range buckets.BucketsUsage {
if isShort {
fmt.Fprintf(table, "%s\n", b.Name) // nolint: errcheck
} else {
t, err := time.Parse(time.RFC3339, b.Created)
if err != nil {
return err
}
fmt.Fprintf(table,
"[%s]\t[%s]\t%s\t%s\t\n", t.Format(printDate), b.Region, humanize.IBytes(uint64(b.Usage)), b.Name) // nolint: errcheck
}
table.Flush()
if isRecursive {
if err = sosClient.setZone(b.Region); err != nil {
return err
}
listObjects(sosClient, b.Name, "", isRecursive, isShort)
}
}
return nil
}
func displayBucket(sosClient *sosClient, path string, isRecursive, isShort bool) error {
isDir := strings.HasSuffix(path, "/")
path = strings.Trim(path, "/")
splitPath := strings.Split(path, "/")
bucket := splitPath[0]
prefix := filepath.Join(splitPath[1:]...)
if isDir && len(prefix) > 1 {
prefix = prefix + "/"
}
zone, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(zone); err != nil {
return err
}
listObjects(sosClient, bucket, prefix, isRecursive, isShort)
return nil
}
func listObjects(sosClient *sosClient, bucket, prefix string, isRecursive, isShort bool) {
table := tabwriter.NewWriter(os.Stdout, 10, 0, 1, ' ', tabwriter.TabIndent)
for object := range sosClient.ListObjectsV2(bucket, prefix, isRecursive, gContext.Done()) {
table.Flush()
if object.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", object.Err)
continue
}
if isShort {
fmt.Fprintf(table, "%s/%s\n", bucket, object.Key) // nolint: errcheck
continue
}
if object.LastModified.IsZero() {
fmt.Fprintf(table,
"%s\t%s\t%s/%s\t\n",
strings.Repeat(" ", 25),
"DIR",
bucket, object.Key) // nolint: errcheck
continue
}
fmt.Fprintf(table,
"[%s]\t%s\t%s/%s\t\n",
object.LastModified.Format(printDate),
humanize.IBytes(uint64(object.Size)),
bucket, object.Key) // nolint: errcheck
}
table.Flush()
}
func init() {
sosCmd.AddCommand(sosListCmd)
sosListCmd.Flags().BoolP("recursive", "r", false, "List recursively")
sosListCmd.Flags().BoolP("short", "S", false, "List in short format")
}