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

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandBucketCreate{})
  13. }
  14. type commandBucketCreate struct {
  15. }
  16. func (c *commandBucketCreate) Name() string {
  17. return "bucket.create"
  18. }
  19. func (c *commandBucketCreate) Help() string {
  20. return `create a bucket with a given name
  21. Example:
  22. bucket.create -name <bucket_name> -replication 001
  23. `
  24. }
  25. func (c *commandBucketCreate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. bucketName := bucketCommand.String("name", "", "bucket name")
  28. replication := bucketCommand.String("replication", "", "replication setting for the bucket")
  29. if err = bucketCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. if *bucketName == "" {
  33. return fmt.Errorf("empty bucket name")
  34. }
  35. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  37. if err != nil {
  38. return fmt.Errorf("get filer configuration: %v", err)
  39. }
  40. filerBucketsPath := resp.DirBuckets
  41. println("create bucket under", filerBucketsPath)
  42. entry := &filer_pb.Entry{
  43. Name: *bucketName,
  44. IsDirectory: true,
  45. Attributes: &filer_pb.FuseAttributes{
  46. Mtime: time.Now().Unix(),
  47. Crtime: time.Now().Unix(),
  48. FileMode: uint32(0777 | os.ModeDir),
  49. Collection: *bucketName,
  50. Replication: *replication,
  51. },
  52. }
  53. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  54. Directory: filerBucketsPath,
  55. Entry: entry,
  56. }); err != nil {
  57. return err
  58. }
  59. println("created bucket", *bucketName)
  60. return nil
  61. })
  62. return err
  63. }