|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package cmdline
-
- import (
- "fmt"
- "os"
- "strconv"
-
- "gitlink.org.cn/cloudream/client/internal/config"
- "gitlink.org.cn/cloudream/client/internal/services"
- )
-
- type Commandline struct {
- svc *services.Service
- }
-
- func NewCommandline(svc *services.Service) (*Commandline, error) {
- return &Commandline{
- svc: svc,
- }, nil
- }
-
- func (c *Commandline) DispatchCommand(cmd string, args []string) {
- switch cmd {
- case "read":
- objectID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
-
- if err := c.Read(args[0], objectID); err != nil {
- fmt.Printf("read failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "write":
- bucketID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
- repNum, _ := strconv.Atoi(args[3])
- if repNum <= 0 || repNum > config.Cfg().MaxReplicateNumber {
- fmt.Printf("replicate number should not be more than %d", config.Cfg().MaxReplicateNumber)
- os.Exit(1)
- }
-
- if err := c.RepWrite(args[0], bucketID, args[2], repNum); err != nil {
- fmt.Printf("rep write failed, err: %s", err.Error())
- os.Exit(1)
- }
- case "ecWrite":
- bucketID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
- if err := c.EcWrite(args[0], bucketID, args[2], args[3]); err != nil {
- fmt.Printf("ec write failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "storage":
- if len(args) == 0 {
- fmt.Printf("need more arg")
- os.Exit(1)
- }
- cmd := args[0]
-
- switch cmd {
- case "move":
- objectID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
- stgID, err := strconv.Atoi(args[2])
- if err != nil {
- fmt.Printf("invalid storage id %s, err: %s", args[2], err.Error())
- os.Exit(1)
- }
-
- if err := c.MoveObjectToStorage(objectID, stgID); err != nil {
- fmt.Printf("move failed, err: %s", err.Error())
- os.Exit(1)
- }
- }
-
- case "bucket":
- if len(args) == 0 {
- fmt.Printf("need more arg")
- os.Exit(1)
- }
- cmd := args[0]
-
- switch cmd {
- case "ls":
- if err := c.ListUserBuckets(); err != nil {
- fmt.Printf("get user buckets failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "new":
- if err := c.CreateBucket(args[1]); err != nil {
- fmt.Printf("create bucket failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "delete":
- bucketID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
-
- if err := c.DeleteBucket(bucketID); err != nil {
- fmt.Printf("delete bucket failed, err: %s", err.Error())
- os.Exit(1)
- }
- }
-
- case "object":
- if len(args) == 0 {
- fmt.Printf("need more arg")
- os.Exit(1)
- }
-
- switch args[0] {
- case "ls":
- bucketID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
-
- if err := c.ListBucketObjects(bucketID); err != nil {
- fmt.Printf("get bucket objects failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "update":
- objectID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
-
- if err := c.UpdateRepObject(objectID, args[2]); err != nil {
- fmt.Printf("delete object failed, err: %s", err.Error())
- os.Exit(1)
- }
-
- case "delete":
- objectID, err := strconv.Atoi(args[1])
- if err != nil {
- fmt.Printf("invalid object id %s, err: %s", args[1], err.Error())
- os.Exit(1)
- }
-
- if err := c.DeleteObject(objectID); err != nil {
- fmt.Printf("delete object failed, err: %s", err.Error())
- os.Exit(1)
- }
- }
- }
-
- }
|