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.

78 lines
1.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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, &commandS3BucketList{})
  12. }
  13. type commandS3BucketList struct {
  14. }
  15. func (c *commandS3BucketList) Name() string {
  16. return "s3.bucket.list"
  17. }
  18. func (c *commandS3BucketList) Help() string {
  19. return `list all buckets
  20. `
  21. }
  22. func (c *commandS3BucketList) 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. _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  28. if parseErr != nil {
  29. return parseErr
  30. }
  31. var filerBucketsPath string
  32. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  33. if err != nil {
  34. return fmt.Errorf("read buckets: %v", err)
  35. }
  36. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  37. if entry.Attributes.Replication == "" || entry.Attributes.Replication == "000" {
  38. fmt.Fprintf(writer, " %s\n", entry.Name)
  39. } else {
  40. fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", entry.Name, entry.Attributes.Replication)
  41. }
  42. return nil
  43. }, "", false, math.MaxUint32)
  44. if err != nil {
  45. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  46. }
  47. return err
  48. }
  49. func readFilerBucketsPath(filerClient filer_pb.FilerClient) (filerBucketsPath string, err error) {
  50. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  51. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  52. if err != nil {
  53. return fmt.Errorf("get filer configuration: %v", err)
  54. }
  55. filerBucketsPath = resp.DirBuckets
  56. return nil
  57. })
  58. return filerBucketsPath, err
  59. }