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.

60 lines
1.0 KiB

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. "code.google.com/p/weed-fs/go/glog"
  5. "fmt"
  6. "os"
  7. )
  8. func init() {
  9. cmdShell.Run = runShell // break init cycle
  10. }
  11. var cmdShell = &Command{
  12. UsageLine: "shell",
  13. Short: "run interactive commands, now just echo",
  14. Long: `run interactive commands.
  15. `,
  16. }
  17. var ()
  18. func runShell(command *Command, args []string) bool {
  19. r := bufio.NewReader(os.Stdin)
  20. o := bufio.NewWriter(os.Stdout)
  21. e := bufio.NewWriter(os.Stderr)
  22. prompt := func() {
  23. var err error
  24. if _, err = o.WriteString("> "); err != nil {
  25. glog.V(0).Infoln("error writing to stdout:", err)
  26. }
  27. if err = o.Flush(); err != nil {
  28. glog.V(0).Infoln("error flushing stdout:", err)
  29. }
  30. }
  31. readLine := func() string {
  32. ret, err := r.ReadString('\n')
  33. if err != nil {
  34. fmt.Fprint(e, err)
  35. os.Exit(1)
  36. }
  37. return ret
  38. }
  39. execCmd := func(cmd string) int {
  40. if cmd != "" {
  41. if _, err := o.WriteString(cmd); err != nil {
  42. glog.V(0).Infoln("error writing to stdout:", err)
  43. }
  44. }
  45. return 0
  46. }
  47. cmd := ""
  48. for {
  49. prompt()
  50. cmd = readLine()
  51. execCmd(cmd)
  52. }
  53. }