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.

122 lines
2.6 KiB

5 years ago
5 years ago
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"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  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(options.GrpcDialOption, "shell", 0, strings.Split(*options.Masters, ",")),
  40. option: options,
  41. }
  42. }
  43. func (ce *CommandEnv) parseUrl(input string) (path string, err error) {
  44. if strings.HasPrefix(input, "http") {
  45. err = fmt.Errorf("http://<filer>:<port> prefix is not supported any more")
  46. return
  47. }
  48. if !strings.HasPrefix(input, "/") {
  49. input = filepath.ToSlash(filepath.Join(ce.option.Directory, input))
  50. }
  51. return input, err
  52. }
  53. func (ce *CommandEnv) isDirectory(path string) bool {
  54. return ce.checkDirectory(path) == nil
  55. }
  56. func (ce *CommandEnv) checkDirectory(path string) error {
  57. dir, name := util.FullPath(path).DirAndName()
  58. exists, err := filer_pb.Exists(ce, dir, name, true)
  59. if !exists {
  60. return fmt.Errorf("%s is not a directory", path)
  61. }
  62. return err
  63. }
  64. func (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  65. filerGrpcAddress := fmt.Sprintf("%s:%d", ce.option.FilerHost, ce.option.FilerPort+10000)
  66. return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn)
  67. }
  68. func (ce *CommandEnv) AdjustedUrl(hostAndPort string) string {
  69. return hostAndPort
  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. }