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.

71 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. err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  36. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  37. if err != nil {
  38. return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err)
  39. }
  40. filerBucketsPath := resp.DirBuckets
  41. if _, err := client.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{
  42. Directory: filerBucketsPath,
  43. Name: *bucketName,
  44. IsDeleteData: false,
  45. IsRecursive: true,
  46. IgnoreRecursiveError: true,
  47. }); err != nil {
  48. return err
  49. }
  50. return nil
  51. })
  52. return err
  53. }