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.

69 lines
1.6 KiB

4 years ago
4 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "io"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandCollectionDelete{})
  11. }
  12. type commandCollectionDelete struct {
  13. }
  14. func (c *commandCollectionDelete) Name() string {
  15. return "collection.delete"
  16. }
  17. func (c *commandCollectionDelete) Help() string {
  18. return `delete specified collection
  19. collection.delete -collection <collection_name> -force
  20. `
  21. }
  22. func (c *commandCollectionDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. colDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  24. collectionName := colDeleteCommand.String("collection", "", "collection to delete. Use '_default_' for the empty-named collection.")
  25. applyBalancing := colDeleteCommand.Bool("force", false, "apply the collection")
  26. if err = colDeleteCommand.Parse(args); err != nil {
  27. return nil
  28. }
  29. if err = commandEnv.confirmIsLocked(); err != nil {
  30. return
  31. }
  32. if *collectionName == "" {
  33. return fmt.Errorf("empty collection name is not allowed")
  34. }
  35. if *collectionName == "_default_" {
  36. *collectionName = ""
  37. }
  38. if !*applyBalancing {
  39. fmt.Fprintf(writer, "collection '%s' will be deleted. Use -force to apply the change.\n", *collectionName)
  40. return nil
  41. }
  42. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  43. _, err = client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  44. Name: *collectionName,
  45. })
  46. return err
  47. })
  48. if err != nil {
  49. return
  50. }
  51. fmt.Fprintf(writer, "collection %s is deleted.\n", *collectionName)
  52. return nil
  53. }