command_collection_delete.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  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) HasTag(CommandTag) bool {
  23. return false
  24. }
  25. func (c *commandCollectionDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. colDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. collectionName := colDeleteCommand.String("collection", "", "collection to delete. Use '_default_' for the empty-named collection.")
  28. applyBalancing := colDeleteCommand.Bool("force", false, "apply the collection")
  29. if err = colDeleteCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. infoAboutSimulationMode(writer, *applyBalancing, "-force")
  33. if err = commandEnv.confirmIsLocked(args); err != nil {
  34. return
  35. }
  36. if *collectionName == "" {
  37. return fmt.Errorf("empty collection name is not allowed")
  38. }
  39. if *collectionName == "_default_" {
  40. *collectionName = ""
  41. }
  42. if !*applyBalancing {
  43. fmt.Fprintf(writer, "collection '%s' will be deleted. Use -force to apply the change.\n", *collectionName)
  44. return nil
  45. }
  46. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  47. _, err = client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  48. Name: *collectionName,
  49. })
  50. return err
  51. })
  52. if err != nil {
  53. return
  54. }
  55. fmt.Fprintf(writer, "collection %s is deleted.\n", *collectionName)
  56. return nil
  57. }