command_s3_bucket_delete.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "io"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandS3BucketDelete{})
  12. }
  13. type commandS3BucketDelete struct {
  14. }
  15. func (c *commandS3BucketDelete) Name() string {
  16. return "s3.bucket.delete"
  17. }
  18. func (c *commandS3BucketDelete) Help() string {
  19. return `delete a bucket by a given name
  20. s3.bucket.delete -name <bucket_name>
  21. `
  22. }
  23. func (c *commandS3BucketDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  25. bucketName := bucketCommand.String("name", "", "bucket name")
  26. if err = bucketCommand.Parse(args); err != nil {
  27. return nil
  28. }
  29. if *bucketName == "" {
  30. return fmt.Errorf("empty bucket name")
  31. }
  32. _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  33. if parseErr != nil {
  34. return parseErr
  35. }
  36. var filerBucketsPath string
  37. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  38. if err != nil {
  39. return fmt.Errorf("read buckets: %v", err)
  40. }
  41. // delete the collection directly first
  42. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  43. _, err = client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  44. Name: getCollectionName(commandEnv, *bucketName),
  45. })
  46. return err
  47. })
  48. if err != nil {
  49. return
  50. }
  51. return filer_pb.Remove(commandEnv, filerBucketsPath, *bucketName, false, true, true, false, nil)
  52. }