You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandBucketDelete{})
  11. }
  12. type commandBucketDelete struct {
  13. }
  14. func (c *commandBucketDelete) Name() string {
  15. return "bucket.delete"
  16. }
  17. func (c *commandBucketDelete) Help() string {
  18. return `delete a bucket by a given name
  19. bucket.delete -name <bucket_name>
  20. `
  21. }
  22. func (c *commandBucketDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  24. bucketName := bucketCommand.String("name", "", "bucket name")
  25. if err = bucketCommand.Parse(args); err != nil {
  26. return nil
  27. }
  28. if *bucketName == "" {
  29. return fmt.Errorf("empty bucket name")
  30. }
  31. filerServer, filerPort, _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  32. if parseErr != nil {
  33. return parseErr
  34. }
  35. ctx := context.Background()
  36. err = commandEnv.withFilerClient(ctx, filerServer, filerPort, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  37. resp, err := client.GetFilerConfiguration(ctx, &filer_pb.GetFilerConfigurationRequest{})
  38. if err != nil {
  39. return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err)
  40. }
  41. filerBucketsPath := resp.DirBuckets
  42. if _, err := client.DeleteEntry(ctx, &filer_pb.DeleteEntryRequest{
  43. Directory: filerBucketsPath,
  44. Name: *bucketName,
  45. IsDeleteData: false,
  46. IsRecursive: true,
  47. IgnoreRecursiveError: true,
  48. }); err != nil {
  49. return err
  50. }
  51. return nil
  52. })
  53. return err
  54. }