| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package main
-
- import (
- "bufio"
- "fmt"
- "io"
- "os"
- )
-
- type Context struct {
- stdout io.Writer
- stderr io.Writer
- stdin io.Reader
- }
-
- type GetCommandResult int
-
- const (
- GetCommandResultOk GetCommandResult = iota
- GetCommandResultError
- )
-
- func getCommand(ctx *Context) (string, error) {
- fmt.Print("$ ")
- command_line, err := bufio.NewReader(ctx.stdin).ReadString('\n')
- if err != nil {
- return "", err
- }
- command, err := parseCommand(ctx, command_line)
- if err != nil {
- return "", err
- }
- return command, nil
- }
-
- func parseCommand(ctx *Context, command_line string) (string, error) {
- _ = ctx
- return command_line[:len(command_line)-1], nil
- }
-
- func handleCommand(ctx Context, command string) {
- fmt.Fprintln(ctx.stderr, command+": command not found")
- }
-
- func main() {
- ctx := Context{
- stdout: os.Stdout,
- stderr: os.Stderr,
- stdin: os.Stdin,
- }
- for {
- command, err := getCommand(&ctx)
- if err != nil {
- fmt.Fprintln(os.Stderr, "Error reading input: ", err)
- os.Exit(1)
- }
- handleCommand(ctx, command)
- }
- }
|