-
Notifications
You must be signed in to change notification settings - Fork 18
/
storage_mb.go
94 lines (73 loc) · 2.2 KB
/
storage_mb.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
package cmd
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/spf13/cobra"
)
var storageMbCmd = &cobra.Command{
Use: "mb sos://BUCKET",
Aliases: []string{"create"},
Short: "Create a new bucket",
Long: fmt.Sprintf(`This command creates a new bucket.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&storageShowBucketOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
acl, err := cmd.Flags().GetString("acl")
if err != nil {
return err
}
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptWithZone(zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
if err := storage.createBucket(bucket, acl); err != nil {
return fmt.Errorf("unable to create bucket: %w", err)
}
if !gQuiet {
return output(storage.showBucket(bucket))
}
return nil
},
}
func init() {
storageMbCmd.Flags().String("acl", "",
fmt.Sprintf("canned ACL to set on bucket (%s)", strings.Join(s3BucketCannedACLToStrings(), "|")))
storageMbCmd.Flags().StringP("zone", "z", "", "bucket zone")
storageCmd.AddCommand(storageMbCmd)
}
func (c *storageClient) createBucket(name, acl string) error {
s3Bucket := s3.CreateBucketInput{Bucket: aws.String(name)}
if acl != "" {
if !isInList(s3BucketCannedACLToStrings(), acl) {
return fmt.Errorf("invalid canned ACL %q, supported values are: %s",
acl,
strings.Join(s3BucketCannedACLToStrings(), ", "))
}
s3Bucket.ACL = s3types.BucketCannedACL(acl)
}
_, err := c.CreateBucket(gContext, &s3Bucket)
return err
}