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.

106 lines
2.2 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. _, err := filer_pb.Exists(ce, dir, name, true)
  58. return err
  59. }
  60. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  61. if strings.HasPrefix(entryPath, "http") {
  62. var u *url.URL
  63. u, err = url.Parse(entryPath)
  64. if err != nil {
  65. return
  66. }
  67. filerServer = u.Hostname()
  68. portString := u.Port()
  69. if portString != "" {
  70. filerPort, err = strconv.ParseInt(portString, 10, 32)
  71. }
  72. path = u.Path
  73. } else {
  74. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  75. }
  76. return
  77. }
  78. func findInputDirectory(args []string) (input string) {
  79. input = "."
  80. if len(args) > 0 {
  81. input = args[len(args)-1]
  82. if strings.HasPrefix(input, "-") {
  83. input = "."
  84. }
  85. }
  86. return input
  87. }