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

commandline.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package cmdline
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "gitlink.org.cn/cloudream/client/internal/config"
  7. "gitlink.org.cn/cloudream/client/internal/services"
  8. )
  9. type Commandline struct {
  10. svc *services.Service
  11. }
  12. func NewCommandline(svc *services.Service) (*Commandline, error) {
  13. return &Commandline{
  14. svc: svc,
  15. }, nil
  16. }
  17. func (c *Commandline) DispatchCommand(cmd string, args []string) {
  18. switch cmd {
  19. case "read":
  20. objectID, err := strconv.Atoi(args[1])
  21. if err != nil {
  22. fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
  23. os.Exit(1)
  24. }
  25. if err := c.Read(args[0], objectID); err != nil {
  26. fmt.Printf("read failed, err: %s", err.Error())
  27. os.Exit(1)
  28. }
  29. case "write":
  30. bucketID, err := strconv.Atoi(args[1])
  31. if err != nil {
  32. fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
  33. os.Exit(1)
  34. }
  35. repNum, _ := strconv.Atoi(args[3])
  36. if repNum <= 0 || repNum > config.Cfg().MaxReplicateNumber {
  37. fmt.Printf("replicate number should not be more than %d", config.Cfg().MaxReplicateNumber)
  38. os.Exit(1)
  39. }
  40. if err := c.RepWrite(args[0], bucketID, args[2], repNum); err != nil {
  41. fmt.Printf("rep write failed, err: %s", err.Error())
  42. os.Exit(1)
  43. }
  44. case "ecWrite":
  45. bucketID, err := strconv.Atoi(args[1])
  46. if err != nil {
  47. fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
  48. os.Exit(1)
  49. }
  50. if err := c.EcWrite(args[0], bucketID, args[2], args[3]); err != nil {
  51. fmt.Printf("ec write failed, err: %s", err.Error())
  52. os.Exit(1)
  53. }
  54. case "storage":
  55. if len(args) == 0 {
  56. fmt.Printf("need more arg")
  57. os.Exit(1)
  58. }
  59. cmd := args[0]
  60. switch cmd {
  61. case "move":
  62. objectID, err := strconv.Atoi(args[1])
  63. if err != nil {
  64. fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
  65. os.Exit(1)
  66. }
  67. stgID, err := strconv.Atoi(args[2])
  68. if err != nil {
  69. fmt.Printf("invalid storage id %s, err: %s", args[2], err.Error())
  70. os.Exit(1)
  71. }
  72. if err := c.Move(objectID, stgID); err != nil {
  73. fmt.Printf("move failed, err: %s", err.Error())
  74. os.Exit(1)
  75. }
  76. }
  77. case "bucket":
  78. if len(args) == 0 {
  79. fmt.Printf("need more arg")
  80. os.Exit(1)
  81. }
  82. cmd := args[0]
  83. switch cmd {
  84. case "ls":
  85. if err := c.ListUserBuckets(); err != nil {
  86. fmt.Printf("get user buckets failed, err: %s", err.Error())
  87. os.Exit(1)
  88. }
  89. case "new":
  90. if err := c.CreateBucket(args[1]); err != nil {
  91. fmt.Printf("create bucket failed, err: %s", err.Error())
  92. os.Exit(1)
  93. }
  94. case "delete":
  95. bucketID, err := strconv.Atoi(args[1])
  96. if err != nil {
  97. fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
  98. os.Exit(1)
  99. }
  100. if err := c.DeleteBucket(bucketID); err != nil {
  101. fmt.Printf("delete bucket failed, err: %s", err.Error())
  102. os.Exit(1)
  103. }
  104. }
  105. case "object":
  106. if len(args) == 0 {
  107. fmt.Printf("need more arg")
  108. os.Exit(1)
  109. }
  110. switch args[0] {
  111. case "ls":
  112. bucketID, err := strconv.Atoi(args[1])
  113. if err != nil {
  114. fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
  115. os.Exit(1)
  116. }
  117. if err := c.ListBucketObjects(bucketID); err != nil {
  118. fmt.Printf("get bucket objects failed, err: %s", err.Error())
  119. os.Exit(1)
  120. }
  121. case "update":
  122. objectID, err := strconv.Atoi(args[1])
  123. if err != nil {
  124. fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
  125. os.Exit(1)
  126. }
  127. if err := c.UpdateRepObject(objectID, args[2]); err != nil {
  128. fmt.Printf("delete object failed, err: %s", err.Error())
  129. os.Exit(1)
  130. }
  131. case "delete":
  132. objectID, err := strconv.Atoi(args[1])
  133. if err != nil {
  134. fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
  135. os.Exit(1)
  136. }
  137. if err := c.DeleteObject(objectID); err != nil {
  138. fmt.Printf("delete object failed, err: %s", err.Error())
  139. os.Exit(1)
  140. }
  141. }
  142. }
  143. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。