-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathrb.go
93 lines (76 loc) · 1.96 KB
/
rb.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
package command
import (
"context"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/v2/log"
"github.com/peak/s5cmd/v2/log/stat"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
)
var removeBucketHelpTemplate = `Name:
{{.HelpName}} - {{.Usage}}
Usage:
{{.HelpName}} s3://bucketname
Options:
{{range .VisibleFlags}}{{.}}
{{end}}
Examples:
1. Deletes S3 bucket with given name
> s5cmd {{.HelpName}} s3://bucketname
`
func NewRemoveBucketCommand() *cli.Command {
cmd := &cli.Command{
Name: "rb",
HelpName: "rb",
Usage: "remove bucket",
CustomHelpTemplate: removeBucketHelpTemplate,
Before: func(c *cli.Context) error {
err := validateMBCommand(c) // uses same validation function with make bucket command.
if err != nil {
printError(commandFromContext(c), c.Command.Name, err)
}
return err
},
Action: func(c *cli.Context) (err error) {
defer stat.Collect(c.Command.FullName(), &err)()
return RemoveBucket{
src: c.Args().First(),
op: c.Command.Name,
fullCommand: commandFromContext(c),
storageOpts: NewStorageOpts(c),
}.Run(c.Context)
},
}
cmd.BashComplete = getBashCompleteFn(cmd, true, true)
return cmd
}
// RemoveBucket holds bucket deletion operation flags and states.
type RemoveBucket struct {
src string
op string
fullCommand string
storageOpts storage.Options
}
// Run removes a bucket.
func (b RemoveBucket) Run(ctx context.Context) error {
bucket, err := url.New(b.src)
if err != nil {
printError(b.fullCommand, b.op, err)
return err
}
client, err := storage.NewRemoteClient(ctx, &url.URL{}, b.storageOpts)
if err != nil {
printError(b.fullCommand, b.op, err)
return err
}
if err := client.RemoveBucket(ctx, bucket.Bucket); err != nil {
printError(b.fullCommand, b.op, err)
return err
}
msg := log.InfoMessage{
Operation: b.op,
Source: bucket,
}
log.Info(msg)
return nil
}