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.

124 lines
2.8 KiB

5 years ago
6 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(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, lookupErr := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
  60. Directory: dir,
  61. Name: name,
  62. })
  63. if lookupErr != nil {
  64. return lookupErr
  65. }
  66. if resp.Entry == nil {
  67. return fmt.Errorf("entry not found")
  68. }
  69. if !resp.Entry.IsDirectory {
  70. return fmt.Errorf("not a directory")
  71. }
  72. return nil
  73. })
  74. }
  75. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  76. if strings.HasPrefix(entryPath, "http") {
  77. var u *url.URL
  78. u, err = url.Parse(entryPath)
  79. if err != nil {
  80. return
  81. }
  82. filerServer = u.Hostname()
  83. portString := u.Port()
  84. if portString != "" {
  85. filerPort, err = strconv.ParseInt(portString, 10, 32)
  86. }
  87. path = u.Path
  88. } else {
  89. err = fmt.Errorf("path should have full url http://<filer_server>:<port>/path/to/dirOrFile : %s", entryPath)
  90. }
  91. return
  92. }
  93. func findInputDirectory(args []string) (input string) {
  94. input = "."
  95. if len(args) > 0 {
  96. input = args[len(args)-1]
  97. if strings.HasPrefix(input, "-") {
  98. input = "."
  99. }
  100. }
  101. return input
  102. }