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.7 KiB

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. "regexp"
  9. "strings"
  10. "github.com/peterh/liner"
  11. "sort"
  12. )
  13. var (
  14. line *liner.State
  15. historyPath = "/tmp/weed-shell"
  16. )
  17. func RunShell(options ShellOptions) {
  18. line = liner.NewLiner()
  19. defer line.Close()
  20. line.SetCtrlCAborts(true)
  21. setCompletionHandler()
  22. loadHisotry()
  23. defer saveHisotry()
  24. reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
  25. commandEnv := &commandEnv{
  26. env: make(map[string]string),
  27. masterClient: wdclient.NewMasterClient(context.Background(),
  28. options.GrpcDialOption, "shell", strings.Split(*options.Masters, ",")),
  29. option: options,
  30. }
  31. go commandEnv.masterClient.KeepConnectedToMaster()
  32. commandEnv.masterClient.WaitUntilConnected()
  33. for {
  34. cmd, err := line.Prompt("> ")
  35. if err != nil {
  36. if err != io.EOF {
  37. fmt.Printf("%v\n", err)
  38. }
  39. return
  40. }
  41. cmds := reg.FindAllString(cmd, -1)
  42. if len(cmds) == 0 {
  43. continue
  44. } else {
  45. line.AppendHistory(cmd)
  46. args := make([]string, len(cmds[1:]))
  47. for i := range args {
  48. args[i] = strings.Trim(string(cmds[1+i]), "\"'")
  49. }
  50. cmd := strings.ToLower(cmds[0])
  51. if cmd == "help" || cmd == "?" {
  52. printHelp(cmds)
  53. } else if cmd == "exit" || cmd == "quit" {
  54. return
  55. } else {
  56. for _, c := range commands {
  57. if c.Name() == cmd {
  58. if err := c.Do(args, commandEnv, os.Stdout); err != nil {
  59. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. func printGenericHelp() {
  68. msg :=
  69. `Type: "help <command>" for help on <command>
  70. `
  71. fmt.Print(msg)
  72. sort.Slice(commands, func(i, j int) bool {
  73. return strings.Compare(commands[i].Name(), commands[j].Name()) < 0
  74. })
  75. for _, c := range commands {
  76. fmt.Printf("\t%s %s \n", c.Name(), c.Help())
  77. }
  78. }
  79. func printHelp(cmds []string) {
  80. args := cmds[1:]
  81. if len(args) == 0 {
  82. printGenericHelp()
  83. } else if len(args) > 1 {
  84. fmt.Println()
  85. } else {
  86. cmd := strings.ToLower(args[0])
  87. sort.Slice(commands, func(i, j int) bool {
  88. return strings.Compare(commands[i].Name(), commands[j].Name()) < 0
  89. })
  90. for _, c := range commands {
  91. if c.Name() == cmd {
  92. fmt.Println()
  93. fmt.Printf("\t%s %s \n", c.Name(), c.Help())
  94. fmt.Println()
  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 loadHisotry() {
  110. if f, err := os.Open(historyPath); err == nil {
  111. line.ReadHistory(f)
  112. f.Close()
  113. }
  114. }
  115. func saveHisotry() {
  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. }