shell_liner.go 2.9 KB

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