command_collection_delete.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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) 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. infoAboutSimulationMode(writer, *applyBalancing, "-force")
  30. if err = commandEnv.confirmIsLocked(args); err != nil {
  31. return
  32. }
  33. if *collectionName == "" {
  34. return fmt.Errorf("empty collection name is not allowed")
  35. }
  36. if *collectionName == "_default_" {
  37. *collectionName = ""
  38. }
  39. if !*applyBalancing {
  40. fmt.Fprintf(writer, "collection '%s' will be deleted. Use -force to apply the change.\n", *collectionName)
  41. return nil
  42. }
  43. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  44. _, err = client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  45. Name: *collectionName,
  46. })
  47. return err
  48. })
  49. if err != nil {
  50. return
  51. }
  52. fmt.Fprintf(writer, "collection %s is deleted.\n", *collectionName)
  53. return nil
  54. }