aboutsummaryrefslogtreecommitdiff
path: root/cmd/myshell/main.go
blob: ee725a2c9540e7fcd29402a44dbcb60b57dd23b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewReader(os.Stdin)
	for {
		fmt.Fprint(os.Stdout, "$ ")

		in, err := stdin.ReadString('\n')
		if err != nil {
			fmt.Println(err.Error())
		}

		trim := strings.TrimSpace(in)

		commands := strings.Split(trim, " ")

		switch commands[0] {
		case "exit":
			code, err := strconv.Atoi(commands[1])
			if err != nil {
				fmt.Println(err.Error())
			}
			os.Exit(code)
			return
		case "echo":
			fmt.Println(os.Stdout, "%s\n", strings.Join(commands[1:], " "))
			return
		default:
			fmt.Printf("%s: command not found\n", commands[0])
			return
		}
	}
}