-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpsh.go
67 lines (60 loc) · 1.13 KB
/
simpsh.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ALL HAIL SIMPSH
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("simpsh> ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
err = execInput(input)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
func execInput(input string) error {
// remove the '\n'
input = strings.TrimSuffix(input, "\n")
// split the input to separate the command and arguments
args := strings.Split(input, " ")
// check for built-in commands
switch args[0] {
case "cd":
// change dir to home dir with empty path not yet supported
if len(args) < 2 {
return errors.New("Path required")
}
err := os.Chdir(args[1])
if err != nil {
return err
}
// stop further processing
return nil
case "exit":
os.Exit(0)
case "quit":
os.Exit(0)
case "::version":
fmt.Println("YAAY SIMPSH 1.0")
return nil
}
// execute the cmd and args
cmd := exec.Command(args[0], args[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return err
}
return nil
}