command_collection_delete.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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(args); 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(false, 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. }