shell.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package command
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  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. }