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.

118 lines
2.6 KiB

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