commands.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/url"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/wdclient"
  13. "google.golang.org/grpc"
  14. )
  15. type ShellOptions struct {
  16. Masters *string
  17. GrpcDialOption grpc.DialOption
  18. // shell transient context
  19. FilerHost string
  20. FilerPort int64
  21. Directory string
  22. }
  23. type CommandEnv struct {
  24. env map[string]string
  25. MasterClient *wdclient.MasterClient
  26. option ShellOptions
  27. }
  28. type command interface {
  29. Name() string
  30. Help() string
  31. Do([]string, *CommandEnv, io.Writer) error
  32. }
  33. var (
  34. Commands = []command{}
  35. )
  36. func NewCommandEnv(options ShellOptions) *CommandEnv {
  37. return &CommandEnv{
  38. env: make(map[string]string),
  39. MasterClient: wdclient.NewMasterClient(context.Background(),
  40. options.GrpcDialOption, "shell", strings.Split(*options.Masters, ",")),
  41. option: options,
  42. }
  43. }
  44. func (ce *CommandEnv) parseUrl(input string) (filerServer string, filerPort int64, path string, err error) {
  45. if strings.HasPrefix(input, "http") {
  46. return parseFilerUrl(input)
  47. }
  48. if !strings.HasPrefix(input, "/") {
  49. input = filepath.ToSlash(filepath.Join(ce.option.Directory, input))
  50. }
  51. return ce.option.FilerHost, ce.option.FilerPort, input, err
  52. }
  53. func (ce *CommandEnv) isDirectory(ctx context.Context, filerServer string, filerPort int64, path string) bool {
  54. return ce.checkDirectory(ctx, filerServer, filerPort, path) == nil
  55. }
  56. func (ce *CommandEnv) checkDirectory(ctx context.Context, filerServer string, filerPort int64, path string) error {
  57. dir, name := filer2.FullPath(path).DirAndName()
  58. return ce.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  59. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  60. Directory: dir,
  61. Prefix: name,
  62. StartFromFileName: name,
  63. InclusiveStartFrom: true,
  64. Limit: 1,
  65. })
  66. if listErr != nil {
  67. return listErr
  68. }
  69. if len(resp.Entries) == 0 {
  70. return fmt.Errorf("entry not found")
  71. }
  72. if resp.Entries[0].Name != name {
  73. return fmt.Errorf("not a valid directory, found %s", resp.Entries[0].Name)
  74. }
  75. if !resp.Entries[0].IsDirectory {
  76. return fmt.Errorf("not a directory")
  77. }
  78. return nil
  79. })
  80. }
  81. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  82. if strings.HasPrefix(entryPath, "http") {
  83. var u *url.URL
  84. u, err = url.Parse(entryPath)
  85. if err != nil {
  86. return
  87. }
  88. filerServer = u.Hostname()
  89. portString := u.Port()
  90. if portString != "" {
  91. filerPort, err = strconv.ParseInt(portString, 10, 32)
  92. }
  93. path = u.Path
  94. } else {
  95. err = fmt.Errorf("path should have full url http://<filer_server>:<port>/path/to/dirOrFile : %s", entryPath)
  96. }
  97. return
  98. }
  99. func findInputDirectory(args []string) (input string) {
  100. input = "."
  101. if len(args) > 0 {
  102. input = args[len(args)-1]
  103. if strings.HasPrefix(input, "-") {
  104. input = "."
  105. }
  106. }
  107. return input
  108. }