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.

110 lines
2.3 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) (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 input, err
  51. }
  52. func (ce *CommandEnv) isDirectory(path string) bool {
  53. return ce.checkDirectory(path) == nil
  54. }
  55. func (ce *CommandEnv) checkDirectory(path string) error {
  56. dir, name := util.FullPath(path).DirAndName()
  57. exists, err := filer_pb.Exists(ce, dir, name, true)
  58. if !exists {
  59. return fmt.Errorf("%s is not a directory", path)
  60. }
  61. return err
  62. }
  63. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  64. if strings.HasPrefix(entryPath, "http") {
  65. var u *url.URL
  66. u, err = url.Parse(entryPath)
  67. if err != nil {
  68. return
  69. }
  70. filerServer = u.Hostname()
  71. portString := u.Port()
  72. if portString != "" {
  73. filerPort, err = strconv.ParseInt(portString, 10, 32)
  74. }
  75. path = u.Path
  76. } else {
  77. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  78. }
  79. return
  80. }
  81. func findInputDirectory(args []string) (input string) {
  82. input = "."
  83. if len(args) > 0 {
  84. input = args[len(args)-1]
  85. if strings.HasPrefix(input, "-") {
  86. input = "."
  87. }
  88. }
  89. return input
  90. }