exec.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package chocococon
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type errNoCmd struct {
  7. cmd string
  8. }
  9. func (e errNoCmd) Error() string {
  10. return "unknown command: " + e.cmd
  11. }
  12. var (
  13. errExit = errors.New("exit")
  14. errNoToken = errors.New("no command")
  15. )
  16. var cmds = map[string]func(params []string) error{
  17. "help": help,
  18. "exit": exit,
  19. }
  20. var helpm = map[string]string{
  21. "help": "Shows help on how to use the console",
  22. "exit": "Shuts down the controller and exits the console",
  23. "lsc": "Lists all commands, will execute if nothing is typed in",
  24. }
  25. func init() {
  26. cmds[""] = listcommands
  27. cmds["lsc"] = listcommands
  28. }
  29. func notyetimplemented(_ []string) error {
  30. fmt.Println("not implemented")
  31. return nil
  32. }
  33. func exec(tokens []string) error {
  34. if len(tokens) == 0 {
  35. return errNoToken
  36. }
  37. if val, ok := cmds[tokens[0]]; ok {
  38. if len(tokens) == 1 {
  39. return val([]string{})
  40. } else {
  41. return val(tokens[1:])
  42. }
  43. } else {
  44. return errNoCmd{tokens[0]}
  45. }
  46. }
  47. func help(params []string) error {
  48. fmt.Println("Line Ending: `;`")
  49. fmt.Println("Seperate Parameters with `,`")
  50. fmt.Println("Example: `println,\"Hello World\";")
  51. return nil
  52. }
  53. func exit(params []string) error {
  54. if curCHC != nil {
  55. fmt.Print("Shutting down controller...")
  56. curCHC.Shutdown()
  57. fmt.Print("OK!\n")
  58. } else {
  59. fmt.Println("Shutting down controller... not connected")
  60. }
  61. return errExit
  62. }
  63. func listcommands(params []string) error {
  64. fmt.Println("Parameters noted within [] brackets, seperated by a comma")
  65. fmt.Println("Defaults notated as [value=default]")
  66. fmt.Println("Available commands: ")
  67. for v, _ := range cmds {
  68. if v == "" {
  69. continue
  70. }
  71. if vh, ok := helpm[v]; ok {
  72. fmt.Printf(" * %10s %s\n", v, vh)
  73. continue
  74. }
  75. fmt.Printf(" * %10s\n", v)
  76. }
  77. return nil
  78. }