aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorjet2tlf <jet2tlf@gmail.com>2024-05-31 04:30:28 +0000
committerjet2tlf <jet2tlf@gmail.com>2024-05-31 04:30:28 +0000
commit4af866a988e2cc59482ea35b3b22f8c5ac802862 (patch)
treeed905ea845dc3c2dbb394a8d1a1d37e51f3505a9 /cmd
parent7cb63edf3067159bb54da87de3cde3c81a343d48 (diff)
downloadshell-go-4af866a988e2cc59482ea35b3b22f8c5ac802862.tar.gz
shell-go-4af866a988e2cc59482ea35b3b22f8c5ac802862.zip
pass 7st stage
Diffstat (limited to 'cmd')
-rw-r--r--cmd/myshell/main.go60
1 files changed, 37 insertions, 23 deletions
diff --git a/cmd/myshell/main.go b/cmd/myshell/main.go
index 64e33d2..df4cfb1 100644
--- a/cmd/myshell/main.go
+++ b/cmd/myshell/main.go
@@ -26,33 +26,47 @@ func main() {
switch command[0] {
case "exit":
- code, err := strconv.Atoi(command[1])
- if err != nil {
- fmt.Println(err.Error())
- }
- os.Exit(code)
+ Exit(command[1])
case "echo":
- fmt.Println(strings.Join(command[1:], " "))
+ Echo(command[1:])
case "type":
- switch command[1] {
- case "exit", "echo", "type":
- fmt.Printf("%s is a shell builtin\n", command[1])
- default:
- env := os.Getenv("PATH")
- paths := strings.Split(env, ":")
-
- for _, path := range paths {
- exec := path + "/" + command[1]
-
- if _, err := os.Stat(exec); err == nil {
- fmt.Printf("%s is %s\n\n", command[1], exec)
- return
- }
- }
- fmt.Printf("%s not found\n", command[1])
- }
+ Type(command[1])
default:
fmt.Printf("%s: command not found\n", command[0])
}
}
}
+
+func Echo(message []string) {
+ fmt.Println(strings.Join(message, " "))
+}
+
+func Exit(code string) {
+ exitCode, err := strconv.Atoi(code)
+
+ if err != nil {
+ fmt.Println(err.Error())
+ }
+
+ os.Exit(exitCode)
+}
+
+func Type(command string) {
+ switch command {
+ case "exit", "echo", "type":
+ fmt.Printf("%s is a shell builtin\n", command)
+ default:
+ env := os.Getenv("PATH")
+ paths := strings.Split(env, ":")
+
+ for _, path := range paths {
+ exec := path + "/" + command
+
+ if _, err := os.Stat(exec); err == nil {
+ fmt.Printf("%s is %s\n", command, exec)
+ return
+ }
+ }
+ fmt.Printf("%s not found\n", command)
+ }
+}