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.

83 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. ctx := context.Background()
  32. err = commandEnv.withFilerClient(ctx, filerServer, filerPort, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  33. resp, err := client.GetFilerConfiguration(ctx, &filer_pb.GetFilerConfigurationRequest{})
  34. if err != nil {
  35. return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err)
  36. }
  37. filerBucketsPath := resp.DirBuckets
  38. stream, err := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  39. Directory: filerBucketsPath,
  40. Limit: math.MaxUint32,
  41. })
  42. if err != nil {
  43. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  44. }
  45. for {
  46. resp, recvErr := stream.Recv()
  47. if recvErr != nil {
  48. if recvErr == io.EOF {
  49. break
  50. } else {
  51. return recvErr
  52. }
  53. }
  54. if resp.Entry.Attributes.Replication == "" || resp.Entry.Attributes.Replication == "000" {
  55. fmt.Fprintf(writer, " %s\n", resp.Entry.Name)
  56. } else {
  57. fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", resp.Entry.Name, resp.Entry.Attributes.Replication)
  58. }
  59. }
  60. return nil
  61. })
  62. return err
  63. }