command_collection_list.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "io"
  7. )
  8. func init() {
  9. Commands = append(Commands, &commandCollectionList{})
  10. }
  11. type commandCollectionList struct {
  12. }
  13. func (c *commandCollectionList) Name() string {
  14. return "collection.list"
  15. }
  16. func (c *commandCollectionList) Help() string {
  17. return `list all collections`
  18. }
  19. func (c *commandCollectionList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  20. collections, err := ListCollectionNames(commandEnv, true, true)
  21. if err != nil {
  22. return err
  23. }
  24. for _, c := range collections {
  25. fmt.Fprintf(writer, "collection:\"%s\"\n", c)
  26. }
  27. fmt.Fprintf(writer, "Total %d collections.\n", len(collections))
  28. return nil
  29. }
  30. func ListCollectionNames(commandEnv *CommandEnv, includeNormalVolumes, includeEcVolumes bool) (collections []string, err error) {
  31. var resp *master_pb.CollectionListResponse
  32. ctx := context.Background()
  33. err = commandEnv.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  34. resp, err = client.CollectionList(ctx, &master_pb.CollectionListRequest{
  35. IncludeNormalVolumes: includeNormalVolumes,
  36. IncludeEcVolumes: includeEcVolumes,
  37. })
  38. return err
  39. })
  40. if err != nil {
  41. return
  42. }
  43. for _, c := range resp.Collections {
  44. collections = append(collections, c.Name)
  45. }
  46. return
  47. }