forked from tencentyun/coscli
/
rb.go
executable file
·57 lines (49 loc) · 1.29 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
package cmd
import (
"context"
"coscli/util"
"fmt"
"os"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var rbCmd = &cobra.Command{
Use: "rb",
Short: "Remove bucket",
Long: `Remove bucket
Format:
./coscli rb cos://<bucket-name>-<app-id> -e <endpoint>
Example:
./coscli rb cos://example-1234567890 -e cos.ap-beijing.myqcloud.com`,
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
bucketIDName, cosPath := util.ParsePath(args[0])
if bucketIDName == "" || cosPath != "" {
return fmt.Errorf("Invalid arguments! ")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
bucketIDName, _ := util.ParsePath(args[0])
flagRegion, _ := cmd.Flags().GetString("region")
if param.Endpoint == "" && flagRegion != "" {
param.Endpoint = fmt.Sprintf("cos.%s.myqcloud.com", flagRegion)
}
removeBucket(bucketIDName)
},
}
func init() {
rootCmd.AddCommand(rbCmd)
rbCmd.Flags().StringP("region", "r", "", "Region")
}
func removeBucket(bucketIDName string) {
c := util.CreateClient(&config, ¶m, bucketIDName)
_, err := c.Bucket.Delete(context.Background())
if err != nil {
logger.Fatalln(err)
os.Exit(1)
}
logger.Infof("Delete a empty bucket! name: %s\n", bucketIDName)
}