You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 KiB

5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandBucketList{})
  12. }
  13. type commandBucketList struct {
  14. }
  15. func (c *commandBucketList) Name() string {
  16. return "bucket.list"
  17. }
  18. func (c *commandBucketList) Help() string {
  19. return `list all buckets
  20. `
  21. }
  22. func (c *commandBucketList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  24. if err = bucketCommand.Parse(args); err != nil {
  25. return nil
  26. }
  27. filerServer, filerPort, _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  28. if parseErr != nil {
  29. return parseErr
  30. }
  31. err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  32. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  33. if err != nil {
  34. return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err)
  35. }
  36. filerBucketsPath := resp.DirBuckets
  37. stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{
  38. Directory: filerBucketsPath,
  39. Limit: math.MaxUint32,
  40. })
  41. if err != nil {
  42. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  43. }
  44. for {
  45. resp, recvErr := stream.Recv()
  46. if recvErr != nil {
  47. if recvErr == io.EOF {
  48. break
  49. } else {
  50. return recvErr
  51. }
  52. }
  53. if resp.Entry.Attributes.Replication == "" || resp.Entry.Attributes.Replication == "000" {
  54. fmt.Fprintf(writer, " %s\n", resp.Entry.Name)
  55. } else {
  56. fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", resp.Entry.Name, resp.Entry.Attributes.Replication)
  57. }
  58. }
  59. return nil
  60. })
  61. return err
  62. }