-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_show.go
63 lines (49 loc) · 1.38 KB
/
storage_show.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/storage/sos"
)
func init() {
storageCmd.AddCommand(&cobra.Command{
Use: "show sos://BUCKET/[OBJECT]",
Short: "Show a bucket/object details",
Long: fmt.Sprintf(`This command lists Storage buckets and objects.
Supported output template annotations:
* When showing a bucket: %s
* When showing an object: %s`,
strings.Join(output.TemplateAnnotations(&sos.ShowBucketOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&sos.ShowObjectOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
key string
)
parts := strings.SplitN(args[0], "/", 2)
bucket = parts[0]
if len(parts) > 1 {
key = parts[1]
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
if key == "" {
return printOutput(storage.ShowBucket(gContext, bucket))
}
return printOutput(storage.ShowObject(gContext, bucket, key))
},
})
}