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.

147 lines
2.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/wdclient"
  6. "io"
  7. "os"
  8. "path"
  9. "regexp"
  10. "strings"
  11. "github.com/peterh/liner"
  12. "sort"
  13. )
  14. var (
  15. line *liner.State
  16. historyPath = path.Join(os.TempDir(), "weed-shell")
  17. )
  18. func RunShell(options ShellOptions) {
  19. line = liner.NewLiner()
  20. defer line.Close()
  21. line.SetCtrlCAborts(true)
  22. setCompletionHandler()
  23. loadHistory()
  24. defer saveHistory()
  25. reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
  26. commandEnv := &commandEnv{
  27. env: make(map[string]string),
  28. masterClient: wdclient.NewMasterClient(context.Background(),
  29. options.GrpcDialOption, "shell", strings.Split(*options.Masters, ",")),
  30. option: options,
  31. }
  32. go commandEnv.masterClient.KeepConnectedToMaster()
  33. commandEnv.masterClient.WaitUntilConnected()
  34. for {
  35. cmd, err := line.Prompt("> ")
  36. if err != nil {
  37. if err != io.EOF {
  38. fmt.Printf("%v\n", err)
  39. }
  40. return
  41. }
  42. cmds := reg.FindAllString(cmd, -1)
  43. if len(cmds) == 0 {
  44. continue
  45. } else {
  46. line.AppendHistory(cmd)
  47. args := make([]string, len(cmds[1:]))
  48. for i := range args {
  49. args[i] = strings.Trim(string(cmds[1+i]), "\"'")
  50. }
  51. cmd := strings.ToLower(cmds[0])
  52. if cmd == "help" || cmd == "?" {
  53. printHelp(cmds)
  54. } else if cmd == "exit" || cmd == "quit" {
  55. return
  56. } else {
  57. for _, c := range commands {
  58. if c.Name() == cmd {
  59. if err := c.Do(args, commandEnv, os.Stdout); err != nil {
  60. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. func printGenericHelp() {
  69. msg :=
  70. `Type: "help <command>" for help on <command>
  71. `
  72. fmt.Print(msg)
  73. sort.Slice(commands, func(i, j int) bool {
  74. return strings.Compare(commands[i].Name(), commands[j].Name()) < 0
  75. })
  76. for _, c := range commands {
  77. helpTexts := strings.SplitN(c.Help(), "\n", 2)
  78. fmt.Printf(" %-30s\t# %s \n", c.Name(), helpTexts[0])
  79. }
  80. }
  81. func printHelp(cmds []string) {
  82. args := cmds[1:]
  83. if len(args) == 0 {
  84. printGenericHelp()
  85. } else if len(args) > 1 {
  86. fmt.Println()
  87. } else {
  88. cmd := strings.ToLower(args[0])
  89. sort.Slice(commands, func(i, j int) bool {
  90. return strings.Compare(commands[i].Name(), commands[j].Name()) < 0
  91. })
  92. for _, c := range commands {
  93. if c.Name() == cmd {
  94. fmt.Printf(" %s\t# %s\n", c.Name(), c.Help())
  95. }
  96. }
  97. }
  98. }
  99. func setCompletionHandler() {
  100. line.SetCompleter(func(line string) (c []string) {
  101. for _, i := range commands {
  102. if strings.HasPrefix(i.Name(), strings.ToLower(line)) {
  103. c = append(c, i.Name())
  104. }
  105. }
  106. return
  107. })
  108. }
  109. func loadHistory() {
  110. if f, err := os.Open(historyPath); err == nil {
  111. line.ReadHistory(f)
  112. f.Close()
  113. }
  114. }
  115. func saveHistory() {
  116. if f, err := os.Create(historyPath); err != nil {
  117. fmt.Printf("Error writing history file: %v\n", err)
  118. } else {
  119. line.WriteHistory(f)
  120. f.Close()
  121. }
  122. }