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.

53 lines
781 B

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func init() {
  8. cmdShell.Run = runShell // break init cycle
  9. }
  10. var cmdShell = &Command{
  11. UsageLine: "shell",
  12. Short: "run interactive commands, now just echo",
  13. Long: `run interactive commands.
  14. `,
  15. }
  16. var ()
  17. func runShell(command *Command, args []string) bool {
  18. r := bufio.NewReader(os.Stdin)
  19. o := bufio.NewWriter(os.Stdout)
  20. e := bufio.NewWriter(os.Stderr)
  21. prompt := func() {
  22. o.WriteString("> ")
  23. o.Flush()
  24. }
  25. readLine := func() string {
  26. ret, err := r.ReadString('\n')
  27. if err != nil {
  28. fmt.Fprint(e, err)
  29. os.Exit(1)
  30. }
  31. return ret
  32. }
  33. execCmd := func(cmd string) int {
  34. if cmd != "" {
  35. o.WriteString(cmd)
  36. }
  37. return 0
  38. }
  39. cmd := ""
  40. for {
  41. prompt()
  42. cmd = readLine()
  43. execCmd(cmd)
  44. }
  45. return true
  46. }