-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_create.go
90 lines (68 loc) · 1.92 KB
/
bucket_create.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
package command
import (
"context"
"fmt"
"strings"
"github.com/romantomjak/b2/b2"
)
type CreateBucketCommand struct {
*baseCommand
}
func (c *CreateBucketCommand) Help() string {
helpText := `
Usage: b2 create [options] <bucket-name>
Creates a new bucket belonging to the account used to create it. The name
must be globally unique and there is a limit of 100 buckets per account.
General Options:
` + c.generalOptions() + `
Create Options:
-type
Either "public", meaning that files in this bucket can be downloaded by
anybody, or "private", meaning that you need an authorization token to
download the files. Defaults to "private".
`
return strings.TrimSpace(helpText)
}
func (c *CreateBucketCommand) Synopsis() string {
return "Create a new bucket"
}
func (c *CreateBucketCommand) Name() string { return "create" }
func (c *CreateBucketCommand) Run(args []string) int {
var bucketType string
flags := c.flagSet()
flags.Usage = func() { c.ui.Output(c.Help()) }
flags.StringVar(&bucketType, "type", "private", "Change bucket type")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got only one argument
args = flags.Args()
if l := len(args); l != 1 {
c.ui.Error("This command takes one argument: <bucket-name>")
return 1
}
// Validate bucket type
if bucketType != "public" && bucketType != "private" {
c.ui.Error(`-type must be either "public" or "private"`)
return 1
}
client, err := c.Client()
if err != nil {
c.ui.Error(fmt.Sprintf("Error: %v", err))
return 1
}
// Create the bucket
b := &b2.BucketCreateRequest{
AccountID: client.AccountID,
Name: args[0],
Type: "all" + strings.Title(bucketType),
}
ctx := context.TODO()
bucket, _, err := client.Bucket.Create(ctx, b)
if err != nil {
c.ui.Error(fmt.Sprintf("Error: %v", err))
return 1
}
c.ui.Output(fmt.Sprintf("Bucket %q created with ID %q", bucket.Name, bucket.ID))
return 0
}