-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_mb.go
73 lines (56 loc) · 1.68 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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/storage/sos"
)
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(output.TemplateAnnotations(&sos.ShowBucketOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
acl, err := cmd.Flags().GetString("acl")
if err != nil {
return err
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptWithZone(zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
if err := storage.CreateNewBucket(gContext, bucket, acl); err != nil {
return fmt.Errorf("unable to create bucket: %w", err)
}
if !globalstate.Quiet {
return printOutput(storage.ShowBucket(gContext, bucket))
}
return nil
},
}
func init() {
storageMbCmd.Flags().String("acl", "",
fmt.Sprintf("canned ACL to set on bucket (%s)", strings.Join(sos.BucketCannedACLToStrings(), "|")))
storageMbCmd.Flags().StringP(zoneFlagLong, zoneFlagShort, "", zoneFlagMsg)
storageCmd.AddCommand(storageMbCmd)
}