From 4af866a988e2cc59482ea35b3b22f8c5ac802862 Mon Sep 17 00:00:00 2001 From: jet2tlf Date: Fri, 31 May 2024 01:30:28 -0300 Subject: pass 7st stage --- cmd/myshell/main.go | 60 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'cmd/myshell/main.go') 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) + } +} -- cgit v1.2.3