Started project, created minimal shell
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
// Package executor exec commands
|
||||
package executor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Exec(line string) {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
return
|
||||
}
|
||||
|
||||
args := strings.Fields(line)
|
||||
command := args[0]
|
||||
commandArgs := args[1:]
|
||||
|
||||
if command == "exit" {
|
||||
fmt.Println("Bye Bye my small deer :3")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if command == "cd" {
|
||||
if len(commandArgs) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "cd: argument was skipped")
|
||||
return
|
||||
}
|
||||
if err := os.Chdir(commandArgs[0]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(command, commandArgs...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Execution error: %v\n", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user