main.go 1023B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. )
  8. type Context struct {
  9. stdout io.Writer
  10. stderr io.Writer
  11. stdin io.Reader
  12. }
  13. type GetCommandResult int
  14. const (
  15. GetCommandResultOk GetCommandResult = iota
  16. GetCommandResultError
  17. )
  18. func getCommand(ctx *Context) (string, error) {
  19. fmt.Print("$ ")
  20. command_line, err := bufio.NewReader(ctx.stdin).ReadString('\n')
  21. if err != nil {
  22. return "", err
  23. }
  24. command, err := parseCommand(ctx, command_line)
  25. if err != nil {
  26. return "", err
  27. }
  28. return command, nil
  29. }
  30. func parseCommand(ctx *Context, command_line string) (string, error) {
  31. _ = ctx
  32. return command_line[:len(command_line)-1], nil
  33. }
  34. func handleCommand(ctx Context, command string) {
  35. fmt.Fprintln(ctx.stderr, command+": command not found")
  36. }
  37. func main() {
  38. ctx := Context{
  39. stdout: os.Stdout,
  40. stderr: os.Stderr,
  41. stdin: os.Stdin,
  42. }
  43. for {
  44. command, err := getCommand(&ctx)
  45. if err != nil {
  46. fmt.Fprintln(os.Stderr, "Error reading input: ", err)
  47. os.Exit(1)
  48. }
  49. handleCommand(ctx, command)
  50. }
  51. }