command_collection_list.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  33. resp, err = client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  34. IncludeNormalVolumes: includeNormalVolumes,
  35. IncludeEcVolumes: includeEcVolumes,
  36. })
  37. return err
  38. })
  39. if err != nil {
  40. return
  41. }
  42. for _, c := range resp.Collections {
  43. collections = append(collections, c.Name)
  44. }
  45. return
  46. }