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.

66 lines
1.4 KiB

  1. package shell
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/wdclient"
  5. "google.golang.org/grpc"
  6. "io"
  7. "net/url"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. )
  12. type ShellOptions struct {
  13. Masters *string
  14. GrpcDialOption grpc.DialOption
  15. // shell transient context
  16. FilerHost string
  17. FilerPort int64
  18. Directory string
  19. }
  20. type commandEnv struct {
  21. env map[string]string
  22. masterClient *wdclient.MasterClient
  23. option ShellOptions
  24. }
  25. type command interface {
  26. Name() string
  27. Help() string
  28. Do([]string, *commandEnv, io.Writer) error
  29. }
  30. var (
  31. commands = []command{}
  32. )
  33. func (ce *commandEnv) parseUrl(input string) (filerServer string, filerPort int64, path string, err error) {
  34. if strings.HasPrefix(input, "http") {
  35. return parseFilerUrl(input)
  36. }
  37. if !strings.HasPrefix(input, "/") {
  38. input = filepath.ToSlash(filepath.Join(ce.option.Directory, input))
  39. }
  40. return ce.option.FilerHost, ce.option.FilerPort, input, err
  41. }
  42. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  43. if strings.HasPrefix(entryPath, "http") {
  44. var u *url.URL
  45. u, err = url.Parse(entryPath)
  46. if err != nil {
  47. return
  48. }
  49. filerServer = u.Hostname()
  50. portString := u.Port()
  51. if portString != "" {
  52. filerPort, err = strconv.ParseInt(portString, 10, 32)
  53. }
  54. path = u.Path
  55. } else {
  56. err = fmt.Errorf("path should have full url http://<filer_server>:<port>/path/to/dirOrFile : %s", entryPath)
  57. }
  58. return
  59. }