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.

119 lines
2.7 KiB

5 years ago
5 years ago
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/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  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. err = fmt.Errorf("http://<filer>:<port> prefix is not supported any more")
  45. return
  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 := util.FullPath(path).DirAndName()
  57. return ce.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  58. resp, lookupErr := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  59. Directory: dir,
  60. Name: name,
  61. })
  62. if lookupErr != nil {
  63. return lookupErr
  64. }
  65. if !resp.Entry.IsDirectory {
  66. return fmt.Errorf("not a directory")
  67. }
  68. return nil
  69. })
  70. }
  71. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  72. if strings.HasPrefix(entryPath, "http") {
  73. var u *url.URL
  74. u, err = url.Parse(entryPath)
  75. if err != nil {
  76. return
  77. }
  78. filerServer = u.Hostname()
  79. portString := u.Port()
  80. if portString != "" {
  81. filerPort, err = strconv.ParseInt(portString, 10, 32)
  82. }
  83. path = u.Path
  84. } else {
  85. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  86. }
  87. return
  88. }
  89. func findInputDirectory(args []string) (input string) {
  90. input = "."
  91. if len(args) > 0 {
  92. input = args[len(args)-1]
  93. if strings.HasPrefix(input, "-") {
  94. input = "."
  95. }
  96. }
  97. return input
  98. }