-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
53 lines (41 loc) · 746 Bytes
/
Copy pathmain.go
File metadata and controls
53 lines (41 loc) · 746 Bytes
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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
input = strings.TrimSuffix(input, "\n")
if err = execute(input); err!=nil{
fmt.Println(os.Stderr, err)
}
}
}
var ErrNoPath = errors.New("path required")
func execute(input string) error{
store := strings.Split(input, " ")
switch store[0]{
case "cd":
if len(store)<2{
return ErrNoPath
}else{
return os.Chdir(store[1])
}
case "exit":
os.Exit(0)
}
cmd := exec.Command(store[0], store[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}