CommandParser.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package Terminal
  2. import (
  3. "golang.org/x/crypto/ssh/terminal"
  4. "strings"
  5. "github.com/swzry/go.TSmap"
  6. "fmt"
  7. "git.swzry.com/NSMCServerLauncher/Logger"
  8. "git.swzry.com/NSMCServerLauncher/Utils"
  9. )
  10. const HELP_MAX_LINE_WIDTH = 80
  11. var CommandList TSmap.TSmap
  12. var HelpCommandList string
  13. func CommandSystemInit() {
  14. CommandList = &TSmap.NewTSmap{
  15. ConMap: make(map[interface{}]interface{}),
  16. }
  17. }
  18. func RegistCommand(name string, reginfo RegistedCommand) {
  19. CommandList.Set(name,reginfo)
  20. }
  21. func GenerateHelpCommandList(){
  22. linecnt := 0
  23. CommandList.ForEach(func(k, v interface{}) {
  24. key,ok := k.(string)
  25. if(!ok){
  26. return
  27. }
  28. if len(key) + linecnt + 1 <= HELP_MAX_LINE_WIDTH {
  29. HelpCommandList += " " + key
  30. linecnt += (len(key) + 1)
  31. }else{
  32. HelpCommandList += "\n" + key
  33. linecnt = len(key)
  34. }
  35. })
  36. }
  37. type RegistedCommand struct {
  38. CmdFunc func(args []string, term *terminal.Terminal,context *Utils.UserContextType)
  39. HelpFunc func(topic string, term *terminal.Terminal)
  40. Intro string
  41. Usage string
  42. }
  43. func ExecuteCommandLine(cmdline string, term *terminal.Terminal,context *Utils.UserContextType){
  44. splwords := strings.Split(cmdline," ")
  45. if(splwords[0] == "help"){
  46. GetHelp(splwords[1:],term)
  47. return
  48. }
  49. vf,ok := CommandList.Get(splwords[0])
  50. if(!ok){
  51. term.Write([]byte("NSMC Server Launcher Shell: Invalid Command! Use 'help' command to get help.\n"))
  52. return
  53. }
  54. rcv,ok := vf.(RegistedCommand)
  55. if(!ok){
  56. fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",splwords[0])
  57. term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
  58. return
  59. }
  60. rcv.CmdFunc(splwords[1:],term,context)
  61. term.SetPrompt("NSMC " + context.PWD + " >")
  62. }
  63. func GetHelp(args []string, term *terminal.Terminal){
  64. switch len(args) {
  65. case 1:
  66. vf,ok := CommandList.Get(args[0])
  67. if(!ok){
  68. term.Write([]byte("help: Invalid Command!\n"))
  69. return
  70. }
  71. rcv,ok := vf.(RegistedCommand)
  72. if(!ok){
  73. fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",args[0])
  74. term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
  75. return
  76. }
  77. term.Write([]byte(fmt.Sprintf("%s - %s\n\nUsage:\n%s\n\n",args[0],rcv.Intro,rcv.Usage)))
  78. rcv.HelpFunc("",term)
  79. term.Write([]byte("\n"))
  80. break
  81. case 2:
  82. vf,ok := CommandList.Get(args[0])
  83. if(!ok){
  84. term.Write([]byte("help: Invalid Command!\n"))
  85. return
  86. }
  87. rcv,ok := vf.(RegistedCommand)
  88. if(!ok){
  89. fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",args[0])
  90. term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
  91. return
  92. }
  93. term.Write([]byte(fmt.Sprintf("Help Topic: %s -> %s\n\n",args[0],args[1])))
  94. rcv.HelpFunc(args[1],term)
  95. term.Write([]byte("\n"))
  96. break
  97. default:
  98. term.Write([]byte("You can get detail infomations by using 'help <command>' or 'help <commmand> <topic>'\n\n"))
  99. term.Write([]byte("Available Commands:\n"))
  100. term.Write([]byte(HelpCommandList))
  101. term.Write([]byte("\n\n"))
  102. break
  103. }
  104. }