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.

123 lines
2.7 KiB

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