-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_list.go
151 lines (122 loc) · 3.92 KB
/
storage_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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/flags"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/storage/sos"
"github.com/exoscale/cli/pkg/storage/sos/object"
exoapi "github.com/exoscale/egoscale/v2/api"
)
var storageListCmd = &cobra.Command{
Use: "list [sos://BUCKET[/[PREFIX/]]",
Short: "List buckets and objects",
Long: fmt.Sprintf(`This command lists buckets and their objects.
If no argument is passed, this commands lists existing buckets. If a prefix is
specified (e.g. "sos://my-bucket/.../") the command lists the objects stored
in the bucket under the corresponding prefix.
Supported output template annotations:
* When listing buckets: %s
* When listing objects: %s`,
strings.Join(output.TemplateAnnotations(&sos.ListBucketsItemOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&object.ListObjectsItemOutput{}), ", ")),
Aliases: gListAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
}
if err := flags.ValidateTimestampFlags(cmd); err != nil {
return err
}
return flags.ValidateVersionFlags(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
prefix string
)
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
if len(args) == 0 {
return printOutput(listStorageBuckets(zone))
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
stream, err := cmd.Flags().GetBool("stream")
if err != nil {
return err
}
parts := strings.SplitN(args[0], "/", 2)
bucket = parts[0]
if len(parts) > 1 {
prefix = parts[1]
}
filters, err := flags.TranslateTimeFilterFlagsToFilterFuncs(cmd)
if err != nil {
return err
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
listVersions, err := cmd.Flags().GetBool(flags.Versions)
if err != nil {
return err
}
versionFilters, err := flags.TranslateVersionFilterFlagsToFilterFuncs(cmd)
if err != nil {
return err
}
if listVersions || len(versionFilters) > 0 {
list := storage.ListVersionedObjectsFunc(bucket, prefix, recursive, stream)
return printOutput(storage.ListObjectsVersions(gContext, list, recursive, stream, filters, versionFilters))
}
list := storage.ListObjectsFunc(bucket, prefix, recursive, stream)
return printOutput(storage.ListObjects(gContext, list, recursive, stream, filters))
},
}
func init() {
storageListCmd.Flags().BoolP("recursive", "r", false,
"list bucket recursively")
storageListCmd.Flags().BoolP("stream", "s", false,
"stream listed files instead of waiting for complete listing (useful for large buckets)")
flags.AddVersionsFlags(storageListCmd)
flags.AddTimeFilterFlags(storageListCmd)
flags.AddZoneListFlag(storageListCmd)
storageCmd.AddCommand(storageListCmd)
}
func listStorageBuckets(zone string) (output.Outputter, error) {
out := make(sos.ListBucketsOutput, 0)
// ListSosBucketsUsageWithResponse is a global command, use default zone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone))
res, err := globalstate.EgoscaleClient.ListSosBucketsUsageWithResponse(ctx)
if err != nil {
return nil, err
}
for _, b := range *res.JSON200.SosBucketsUsage {
created := *b.CreatedAt
if err != nil {
return nil, err
}
// Filter the zone if set as flag
if zone == "" || zone == string(*b.ZoneName) {
out = append(out, sos.ListBucketsItemOutput{
Name: *b.Name,
Zone: string(*b.ZoneName),
Size: *b.Size,
Created: created.Format(object.TimestampFormat),
})
}
}
return &out, nil
}