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.

121 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
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  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, pb.AdminShellClient, 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 = util.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 (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  64. filerGrpcAddress := fmt.Sprintf("%s:%d", ce.option.FilerHost, ce.option.FilerPort+10000)
  65. return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn)
  66. }
  67. func (ce *CommandEnv) AdjustedUrl(hostAndPort string) string {
  68. return hostAndPort
  69. }
  70. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  71. if strings.HasPrefix(entryPath, "http") {
  72. var u *url.URL
  73. u, err = url.Parse(entryPath)
  74. if err != nil {
  75. return
  76. }
  77. filerServer = u.Hostname()
  78. portString := u.Port()
  79. if portString != "" {
  80. filerPort, err = strconv.ParseInt(portString, 10, 32)
  81. }
  82. path = u.Path
  83. } else {
  84. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  85. }
  86. return
  87. }
  88. func findInputDirectory(args []string) (input string) {
  89. input = "."
  90. if len(args) > 0 {
  91. input = args[len(args)-1]
  92. if strings.HasPrefix(input, "-") {
  93. input = "."
  94. }
  95. }
  96. return input
  97. }