forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb.go
83 lines (69 loc) · 1.34 KB
/
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
package command
import (
"context"
"fmt"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/log"
"github.com/peak/s5cmd/storage"
"github.com/peak/s5cmd/storage/url"
)
var makeBucketHelpTemplate = `Name:
{{.HelpName}} - {{.Usage}}
Usage:
{{.HelpName}} bucketname
Options:
{{range .VisibleFlags}}{{.}}
{{end}}
Examples:
1. Create a new S3 bucket
> s5cmd {{.HelpName}} newbucket
`
var makeBucketCommand = &cli.Command{
Name: "mb",
HelpName: "mb",
Usage: "make bucket",
CustomHelpTemplate: makeBucketHelpTemplate,
Before: func(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("expected only 1 argument")
}
src := c.Args().First()
bucket, err := url.New(src)
if err != nil {
return err
}
if !bucket.IsBucket() {
return fmt.Errorf("invalid s3 bucket")
}
return nil
},
Action: func(c *cli.Context) error {
return MakeBucket(
c.Context,
c.Command.Name,
c.Args().First(),
)
},
}
// MakeBucket creates bucket.
func MakeBucket(
ctx context.Context,
op string,
src string,
) error {
bucket, err := url.New(src)
if err != nil {
return err
}
client := storage.NewClient(bucket)
err = client.MakeBucket(ctx, bucket.Bucket)
if err != nil {
return err
}
msg := log.InfoMessage{
Operation: op,
Source: bucket,
}
log.Info(msg)
return nil
}