2019-05-31 00:47:54 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-20 23:46:16 +08:00
|
|
|
"flag"
|
2019-05-31 00:47:54 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 16:30:24 +08:00
|
|
|
Commands = append(Commands, &commandCollectionDelete{})
|
2019-05-31 00:47:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandCollectionDelete struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandCollectionDelete) Name() string {
|
|
|
|
return "collection.delete"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandCollectionDelete) Help() string {
|
|
|
|
return `delete specified collection
|
|
|
|
|
2020-11-20 22:50:46 +08:00
|
|
|
collection.delete -collection <collection_name> -force
|
2019-05-31 00:47:54 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
func (c *commandCollectionDelete) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-05-31 00:47:54 +08:00
|
|
|
|
2020-09-20 23:46:16 +08:00
|
|
|
colDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2020-12-14 17:05:20 +08:00
|
|
|
collectionName := colDeleteCommand.String("collection", "", "collection to delete. Use '_default_' for the empty-named collection.")
|
2020-09-20 23:46:16 +08:00
|
|
|
applyBalancing := colDeleteCommand.Bool("force", false, "apply the collection")
|
|
|
|
if err = colDeleteCommand.Parse(args); err != nil {
|
2019-05-31 00:47:54 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-01 05:48:46 +08:00
|
|
|
infoAboutSimulationMode(writer, *applyBalancing, "-force")
|
2019-05-31 00:47:54 +08:00
|
|
|
|
2021-12-11 05:24:38 +08:00
|
|
|
if err = commandEnv.confirmIsLocked(args); err != nil {
|
2021-09-14 13:13:34 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:05:20 +08:00
|
|
|
if *collectionName == "" {
|
|
|
|
return fmt.Errorf("empty collection name is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *collectionName == "_default_" {
|
|
|
|
*collectionName = ""
|
|
|
|
}
|
|
|
|
|
2020-09-20 23:46:16 +08:00
|
|
|
if !*applyBalancing {
|
2020-12-14 17:05:20 +08:00
|
|
|
fmt.Fprintf(writer, "collection '%s' will be deleted. Use -force to apply the change.\n", *collectionName)
|
2020-09-20 23:46:16 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-31 00:47:54 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
|
2020-02-26 13:50:12 +08:00
|
|
|
_, err = client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
|
2020-09-20 23:46:16 +08:00
|
|
|
Name: *collectionName,
|
2019-05-31 00:47:54 +08:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-20 23:47:30 +08:00
|
|
|
fmt.Fprintf(writer, "collection %s is deleted.\n", *collectionName)
|
2019-05-31 00:47:54 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|