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.

120 lines
2.7 KiB

  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. "google.golang.org/grpc"
  9. "io"
  10. "net/url"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  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 (ce *commandEnv) parseUrl(input string) (filerServer string, filerPort int64, path string, err error) {
  37. if strings.HasPrefix(input, "http") {
  38. return parseFilerUrl(input)
  39. }
  40. if !strings.HasPrefix(input, "/") {
  41. input = filepath.ToSlash(filepath.Join(ce.option.Directory, input))
  42. }
  43. return ce.option.FilerHost, ce.option.FilerPort, input, err
  44. }
  45. func (ce *commandEnv) isDirectory(ctx context.Context, filerServer string, filerPort int64, path string) bool {
  46. return ce.checkDirectory(ctx,filerServer,filerPort,path) == nil
  47. }
  48. func (ce *commandEnv) checkDirectory(ctx context.Context, filerServer string, filerPort int64, path string) error {
  49. dir, name := filer2.FullPath(path).DirAndName()
  50. return ce.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  51. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  52. Directory: dir,
  53. Prefix: name,
  54. StartFromFileName: name,
  55. InclusiveStartFrom: true,
  56. Limit: 1,
  57. })
  58. if listErr != nil {
  59. return listErr
  60. }
  61. if len(resp.Entries) == 0 {
  62. return fmt.Errorf("entry not found")
  63. }
  64. if resp.Entries[0].Name != name {
  65. return fmt.Errorf("not a valid directory, found %s", resp.Entries[0].Name)
  66. }
  67. if !resp.Entries[0].IsDirectory {
  68. return fmt.Errorf("not a directory")
  69. }
  70. return nil
  71. })
  72. }
  73. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  74. if strings.HasPrefix(entryPath, "http") {
  75. var u *url.URL
  76. u, err = url.Parse(entryPath)
  77. if err != nil {
  78. return
  79. }
  80. filerServer = u.Hostname()
  81. portString := u.Port()
  82. if portString != "" {
  83. filerPort, err = strconv.ParseInt(portString, 10, 32)
  84. }
  85. path = u.Path
  86. } else {
  87. err = fmt.Errorf("path should have full url http://<filer_server>:<port>/path/to/dirOrFile : %s", entryPath)
  88. }
  89. return
  90. }
  91. func findInputDirectory(args []string) (input string) {
  92. input = "."
  93. if len(args) > 0 {
  94. input = args[len(args)-1]
  95. if strings.HasPrefix(input, "-") {
  96. input = "."
  97. }
  98. }
  99. return input
  100. }